package com.stylefeng.guns.modular.map.controller;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.node.ZTreeNode;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.area.dao.BdapAreaLoyaltyMmMapper;
import com.stylefeng.guns.modular.area.dao.BdapAreaMapper;
import com.stylefeng.guns.modular.area.dao.BdapAreaPopPropDmMapper;
import com.stylefeng.guns.modular.area.dao.BdapAreaPopPropDtMapper;
import com.stylefeng.guns.modular.area.dao.BdapAreaPopPropHmMapper;
import com.stylefeng.guns.modular.area.dao.BdapAreaPopPropQmMapper;
import com.stylefeng.guns.modular.area.dao.BdapAreaStayMmMapper;
import com.stylefeng.guns.modular.area.dao.BdapCellPopPropHmMapper;
import com.stylefeng.guns.modular.area.dao.BdapSubAreaMapper;
import com.stylefeng.guns.modular.area.dao.BdapSubareaLoyaltyMmMapper;
import com.stylefeng.guns.modular.area.dao.BdapSubareaPopPropDmMapper;
import com.stylefeng.guns.modular.area.dao.BdapSubareaPopPropDtMapper;
import com.stylefeng.guns.modular.area.dao.BdapSubareaPopPropHmMapper;
import com.stylefeng.guns.modular.area.dao.BdapSubareaPopPropQmMapper;
import com.stylefeng.guns.modular.area.dao.BdapSubareaStayMmMapper;
import com.stylefeng.guns.modular.area.dao.BdapUserAreaMapper;
import com.stylefeng.guns.modular.area.model.BdapArea;
import com.stylefeng.guns.modular.area.model.BdapAreaLoyaltyMm;
import com.stylefeng.guns.modular.area.model.BdapAreaPopPropDm;
import com.stylefeng.guns.modular.area.model.BdapAreaPopPropDt;
import com.stylefeng.guns.modular.area.model.BdapAreaPopPropHm;
import com.stylefeng.guns.modular.area.model.BdapAreaPopPropQm;
import com.stylefeng.guns.modular.area.model.BdapAreaStayMm;
import com.stylefeng.guns.modular.area.model.BdapCellPopPropHm;
import com.stylefeng.guns.modular.area.model.BdapSubArea;
import com.stylefeng.guns.modular.area.model.BdapSubareaLoyaltyMm;
import com.stylefeng.guns.modular.area.model.BdapSubareaPopPropDm;
import com.stylefeng.guns.modular.area.model.BdapSubareaPopPropDt;
import com.stylefeng.guns.modular.area.model.BdapSubareaPopPropHm;
import com.stylefeng.guns.modular.area.model.BdapSubareaPopPropQm;
import com.stylefeng.guns.modular.area.model.BdapSubareaStayMm;
import com.stylefeng.guns.modular.area.model.BdapUserArea;
import com.stylefeng.guns.modular.area.util.AreaUtil;
import com.stylefeng.guns.modular.map.dao.BdapCellLoyaltyMmMapper;
import com.stylefeng.guns.modular.map.dao.BdapCellStayDmMapper;
import com.stylefeng.guns.modular.map.dao.BdapCellStayMmMapper;
import com.stylefeng.guns.modular.map.dao.ScreeningDao;
import com.stylefeng.guns.modular.map.dao.TraceDayMapper;
import com.stylefeng.guns.modular.map.dao.TraceHourMapper;
import com.stylefeng.guns.modular.map.dao.TraceMonthMapper;
import com.stylefeng.guns.modular.map.model.AgeSegmentation;
import com.stylefeng.guns.modular.map.model.AreaDmModel;
import com.stylefeng.guns.modular.map.model.AreaDtModel;
import com.stylefeng.guns.modular.map.model.AreaHmModel;
import com.stylefeng.guns.modular.map.model.BdapCellLoyaltyMm;
import com.stylefeng.guns.modular.map.model.BdapCellStayDm;
import com.stylefeng.guns.modular.map.model.BdapCellStayMm;
import com.stylefeng.guns.modular.map.model.Consumption;
import com.stylefeng.guns.modular.map.model.CumulativePassenger;
import com.stylefeng.guns.modular.map.model.LonLat;
import com.stylefeng.guns.modular.map.model.LoyaltySituation;
import com.stylefeng.guns.modular.map.model.MyTimeType;
import com.stylefeng.guns.modular.map.model.NativePopulation;
import com.stylefeng.guns.modular.map.model.OriginPopulation;
import com.stylefeng.guns.modular.map.model.PassengerFlowUtil;
import com.stylefeng.guns.modular.map.model.PassingDensity;
import com.stylefeng.guns.modular.map.model.PassingPopulation;
import com.stylefeng.guns.modular.map.model.PeakPassenger;
import com.stylefeng.guns.modular.map.model.PermanentPeople;
import com.stylefeng.guns.modular.map.model.RealPassenger;
import com.stylefeng.guns.modular.map.model.Sex;
import com.stylefeng.guns.modular.map.model.StaySituation;
import com.stylefeng.guns.modular.map.model.StayTimeRanking;
import com.stylefeng.guns.modular.map.model.SubAreaDmModel;
import com.stylefeng.guns.modular.map.model.SubAreaDtModel;
import com.stylefeng.guns.modular.map.model.SubAreaHmModel;
import com.stylefeng.guns.modular.map.model.TerminalPopulation;
import com.stylefeng.guns.modular.map.model.TraceDay;
import com.stylefeng.guns.modular.map.model.TraceHour;
import com.stylefeng.guns.modular.map.model.TraceMonth;
import com.stylefeng.guns.modular.map.model.TravelPopulation;
import com.stylefeng.guns.modular.map.model.UserCounty;
import com.stylefeng.guns.modular.map.model.WorkForce;
import com.stylefeng.guns.modular.util.CityCodeUtil;
import com.stylefeng.guns.modular.util.MySplitUtilToList;
import com.stylefeng.guns.modular.util.StringUtil;
import com.stylefeng.guns.modular.util.TimeUtil;

@Controller
@RequestMapping("/screening")
public class AreaManageController extends BaseController{
	private String PREFIX = "/map/screening/";
	/**
	 * 用户区域授权表mapper
	 */
	@Autowired
	BdapUserAreaMapper userAreaMapper;
	/**
	 * 区域表mapper
	 */
	@Autowired
	BdapAreaMapper areaMapper;
	@Autowired
	BdapCellPopPropHmMapper cellpopHmMapper;

	/**
	 * 子区域表mapper
	 */
	@Autowired
	BdapSubAreaMapper subareaMapper;
	@Autowired
	private TraceDayMapper traceDayMapper;
	@Autowired
	private TraceMonthMapper traceMonthMapper;
	@Autowired
	private TraceHourMapper traceHourMapper;
	@Autowired
	ScreeningDao screenMapper;
	@Autowired
	private BdapCellStayDmMapper bdapCellStayDmMaper;
	@Autowired
	private BdapCellStayMmMapper bdapCellStayMmMaper;
	@Autowired
	private BdapCellLoyaltyMmMapper bdapCellLoyaltyMmMapper;
	@Autowired
	private BdapAreaPopPropQmMapper areaQmMapper;
	@Autowired
	private BdapAreaPopPropHmMapper areaHmMapper;
	@Autowired
	private BdapSubareaPopPropQmMapper subAreaQmMapper;
	@Autowired
	private BdapSubareaPopPropHmMapper subAreaHmMapper;
	@Autowired
	private BdapSubareaPopPropDmMapper subAreaDmMapper;
	@Autowired
	private BdapAreaPopPropDmMapper areaDmMapper;
	@Autowired
	private BdapAreaPopPropDtMapper areaDtMapper;
	@Autowired
	private BdapSubareaPopPropDtMapper subAreaDtMapper;
	@Autowired
	private BdapAreaStayMmMapper areaStayMmMapper;
	@Autowired
	private BdapSubareaStayMmMapper subAreaStayMmMapper;
	@Autowired
	private BdapAreaLoyaltyMmMapper areaLoyaltyMapper;
	@Autowired
	private BdapSubareaLoyaltyMmMapper subAreaLoyaltyMapper;
	/**
	 * 获取授权区域的tree列表
	 */
	@RequestMapping(value = "/tree")
	@ResponseBody
	public List<ZTreeNode> tree() {
		int userId = ShiroKit.getUser().getId();// 当前用户ID
		Wrapper<BdapUserArea> userAreaWrapper = new EntityWrapper<BdapUserArea>();// 用户区域查询条件类
		userAreaWrapper.eq("USERID", userId);
		List<BdapUserArea> userAreaList = userAreaMapper.selectList(userAreaWrapper);// 用户授权的区域ID
		List<BdapArea> areaList = new ArrayList<BdapArea>();// 区域表
		List<BdapSubArea> subAreaList = new ArrayList<BdapSubArea>();
		// 根据用户授权区域，查询区域下的子区域
		List<ZTreeNode> zTreeNodes = new ArrayList<ZTreeNode>();// ZTree结构
		Map<String, String> cityMap = new HashMap<String, String>();// 存放地市
		// Wrapper<BdapArea> areaWrapper=new EntityWrapper<BdapArea>();
		// Wrapper<BdapSubArea> subAreaWrapper=new EntityWrapper<BdapSubArea>();
		// if(userAreaList.size()>0){
		// List<String> areaIds=new ArrayList<String>();
		// for(BdapUserArea area:userAreaList){
		// areaIds.add(area.getAreaid()+"");
		// }
		// subAreaWrapper.in("PID", areaIds);
		// areaWrapper.in("ID", areaIds);
		// subAreaList=subareaMapper.selectList(subAreaWrapper);
		// areaList=areaMapper.selectList(areaWrapper);

		if (ShiroKit.isAdmin()) {
			subAreaList = subareaMapper.selectAdminSubAreaList();
			areaList = areaMapper.selectAdminAreaList();
		} else {
			subAreaList = subareaMapper.selectSubAreaList(userId);
			areaList = areaMapper.selectAreaList(userId);
		}
		// }
		// if(ShiroKit.isAdmin()){
		// areaWrapper=new EntityWrapper<BdapArea>();
		// subAreaWrapper=new EntityWrapper<BdapSubArea>();
		// subAreaList=subareaMapper.selectList(subAreaWrapper);
		// areaList=areaMapper.selectList(areaWrapper);
		// }
		// 根据地市，区域和子区域的关系，按照前端要求，封装成树形结构
		int i = 0;
		for (BdapArea area : areaList) {
			ZTreeNode treeNode = new ZTreeNode();
			if (!cityMap.containsKey(area.getCity())) {
				ZTreeNode cityNode = new ZTreeNode();
				cityMap.put(area.getCity(), CityCodeUtil.getCityCode(area.getCity()));
				cityNode.setId(Long.parseLong(cityMap.get(area.getCity())));
				cityNode.setpId(0L);
				cityNode.setName(area.getCity());
				zTreeNodes.add(cityNode);
			}
			if (i == 0) {
				treeNode.setChecked(true);
			}
			treeNode.setId(area.getId());
			treeNode.setpId(Long.parseLong(cityMap.get(area.getCity())));
			treeNode.setName(area.getName());
			zTreeNodes.add(treeNode);
			i++;
		}
		for (BdapSubArea subArea : subAreaList) {
			ZTreeNode treeNode = new ZTreeNode();
			treeNode.setId(subArea.getId());
			treeNode.setpId(subArea.getPid());
			treeNode.setName(subArea.getName());
			zTreeNodes.add(treeNode);
		}
		return zTreeNodes;
	}

	/**
	 * 跳转到地图筛选首页
	 */
	@RequestMapping("")
	public String index(Model model, String areaId, String subAreaId, String areaName) {
		model.addAttribute("areaId", areaId);
		model.addAttribute("subAreaId", subAreaId);
		model.addAttribute("areaName", areaName);
		return PREFIX + "screening.html";
	}

	/**
	 * 跳转到添加地图筛选
	 */
	@RequestMapping("/screening_add")
	public String screeningAdd() {
		return PREFIX + "screening_add.html";
	}

	/**
	 * 跳转到修改地图筛选
	 */
	@RequestMapping("/screening_update/{screeningId}")
	public String screeningUpdate(@PathVariable Integer screeningId, Model model) {
		return PREFIX + "screening_edit.html";
	}

	/**
	 * 获取地图筛选列表
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Object list(String condition) {
		return null;
	}

	/**
	 * 新增地图筛选
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Object add() {
		return SUCCESS_TIP;
	}

	/**
	 * 删除地图筛选
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Object delete() {
		return SUCCESS_TIP;
	}

	/**
	 * 修改地图筛选
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Object update() {
		return SUCCESS_TIP;
	}

	/**
	 * 地图筛选详情
	 */
	@RequestMapping(value = "/detail")
	@ResponseBody
	public Object detail() {
		return null;
	}

	/**
	 * 实时客流
	 * 
	 * @Description: TODO
	 * @param @param
	 *            beginTime
	 * @param @param
	 *            upTime
	 * @param @param
	 *            timeType
	 * @param @param
	 *            peopleType
	 * @param @param
	 *            areaId
	 * @param @return
	 * @return Object
	 * @throws @author
	 *             zhangyaoyao
	 * @date 2017年11月21日
	 */
	@RequestMapping(value = "/realPassenger")
	@ResponseBody
	public Object realPassenger(String beginTime, String areaId, String subAreaId ,String timeType) {
		if (ToolUtil.isEmpty(beginTime) || ToolUtil.isEmpty(timeType)) {
			return null;
		}
		RealPassenger realPassenger = new RealPassenger();
		Map<String, String> userTotalMap = new LinkedHashMap<>();// 存放实时客流数据<时间，人数>
		Map<String, String> ForcastUserTotalMap = new LinkedHashMap<>();// 存放预测客流数据<时间，人数>
		// List<String> userTime = new ArrayList<String>(); //存放实时客流对应的时间
		Set<String> userTime = new TreeSet<String>(); // 存放实时客流对应的时间
		List<String> timeString = new ArrayList<>();// 存放一天的所有时间集合（小时：00,01,02……）
		List<String> minute = new ArrayList<String>();
		minute.add("00");
		minute.add("15");
		minute.add("30");
		minute.add("45");
		String maximum = null;// 查询出的当天最大时间
		List<String> maximumList = new ArrayList<String>();
		if(timeType.equals("分钟")){
			for(int i=0;i<=23;i++){
				String string  = null;
				if(i<10){
					String str = "0";
					string = str + i;
					for(int j=0;j<minute.size();j++){
						String time = string + minute.get(j);
						timeString.add(time);
					}
				}
				if(i>=10){
					String valueOf = String.valueOf(i);
					for(int j=0;j<minute.size();j++){
						String time = valueOf + minute.get(j);
						timeString.add(time);
					}
				}
			}
		}else{
			for (int i = 0; i <= 23; i++) {
				String string = null;
				if (i < 10) {
					String str = "0";
					string = str + i;
					timeString.add(string);
				}
				if (i >= 10) {
					String valueOf = String.valueOf(i);
					timeString.add(valueOf);
				}
			}
		}
		MyTimeType mytimeType = new MyTimeType();
		// 1.根据时间和区域id去区域表查询警戒值，饱和值，警戒值
		// 2.封装到实时客流（类）里面对应的list集合中
		if (ToolUtil.isEmpty(subAreaId)) {
			BdapArea area = areaMapper.selectById(Long.parseLong(areaId));
			if (ToolUtil.isEmpty(area)) {
				return null;
			}
			realPassenger.getRealWarningValue().add(area.getWarningValue());
			realPassenger.getRealSaturationValue().add(area.getSaturationValue());
			realPassenger.getRealServerValue().add(area.getSevereValue());
		} else {
			BdapSubArea subarea = subareaMapper.selectById(Long.parseLong(subAreaId));
			if (ToolUtil.isEmpty(subarea)) {
				return null;
			}
			realPassenger.getRealWarningValue().add(subarea.getWarningValue());
			realPassenger.getRealSaturationValue().add(subarea.getSaturationValue());
			realPassenger.getRealServerValue().add(subarea.getSevereValue());
		}
		Date date = new Date();
		SimpleDateFormat formatHour = null;
		String hour = null;
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		if(timeType.equals("分钟")){
			formatHour = new SimpleDateFormat("HH:mm");
			hour = formatHour.format(date);// 当前时间
			String substringHour = hour.substring(0,2);
			String substringMinute = hour.substring(3,5);
			int parseInt = Integer.parseInt(substringMinute);
			if(parseInt<=15){
				parseInt = 0;
			}else if(15<parseInt&&parseInt<=30){
				parseInt = 15;
			}else if(30<parseInt&&parseInt<=45){
				parseInt = 30;
			}else if(parseInt>45){
				parseInt = 45;
			}
			if(parseInt<=10){
				hour = substringHour + "0" + String.valueOf(parseInt);
			}else{
				hour = substringHour + String.valueOf(parseInt);
			}
		}else{
			formatHour = new SimpleDateFormat("HH");
			hour = formatHour.format(date);
		}
		String formatTime = format.format(date);
		String replaceTime = beginTime.replace("-", "");
		mytimeType.setBeginTime(replaceTime);
		mytimeType.setUpTime(replaceTime);
		if(ToolUtil.isNotEmpty(subAreaId)){
			mytimeType.setSubAreaId(subAreaId);
			if(timeType.equals("分钟")){
				List<BdapSubareaPopPropQm> SubareaQmList = subAreaQmMapper.selectSubAreaPopulationByTime(mytimeType);
				for (BdapSubareaPopPropQm bdapSubareaPopPropQm : SubareaQmList) {
					String totalUsers = bdapSubareaPopPropQm.getTotalUsers();
					String statMinute = bdapSubareaPopPropQm.getStatMinutes();
					statMinute = statMinute.substring(8);
					String prefixMinute = statMinute.substring(0, 2);
					String postfixMinute = statMinute.substring(2, 4);
					userTotalMap.put(statMinute, totalUsers);// 实时客流map
					ForcastUserTotalMap.put(statMinute, "-");// 预测客流map
					String stringHour = prefixMinute + ":" + postfixMinute;
					userTime.add(stringHour);// 时间
					maximumList.add(statMinute);
				}
			}else{
				List<BdapSubareaPopPropHm> areaHmList = subAreaHmMapper.selectSubAreaHourPopulation(mytimeType);
				for (BdapSubareaPopPropHm bdapSubareaPopPropHm : areaHmList) {
					String totalUsers = bdapSubareaPopPropHm.getTotalUsers();
					String statHour = bdapSubareaPopPropHm.getStatHour();
					String substringHour = statHour.substring(8);
					userTotalMap.put(substringHour, totalUsers);// 实时客流map
					ForcastUserTotalMap.put(substringHour, "-");// 预测客流map
					String stringHour = substringHour + ":00";
					userTime.add(stringHour);// 时间
					maximumList.add(substringHour);
				}
			}
		}else{
			mytimeType.setAreaId(areaId);
			if(timeType.equals("分钟")){
				List<BdapAreaPopPropQm> areaMinnuteList = areaQmMapper.selectAreaMinnutePopuplation(mytimeType);
				for (BdapAreaPopPropQm bdapAreaPopPropQm : areaMinnuteList) {
					String totalUsers = bdapAreaPopPropQm.getTotalUsers();
					String statMinute = bdapAreaPopPropQm.getStatMinutes();
					statMinute = statMinute.substring(8);
					String prefixMinute = statMinute.substring(0, 2);
					String postfixMinute = statMinute.substring(2, 4);
					userTotalMap.put(statMinute, totalUsers);// 实时客流map
					ForcastUserTotalMap.put(statMinute, "-");// 预测客流map
					String stringHour = prefixMinute + ":" + postfixMinute;
					userTime.add(stringHour);// 时间
					maximumList.add(statMinute);
				}
			}else{
				List<BdapAreaPopPropHm> areaHourList = areaHmMapper.selectAreaHourPopulation(mytimeType);
				for (BdapAreaPopPropHm bdapAreaPopPropHm : areaHourList) {
					String totalUsers = bdapAreaPopPropHm.getTotalUsers();
					String statHour = bdapAreaPopPropHm.getStatHour();
					String substringHour = statHour.substring(8);
					userTotalMap.put(substringHour, totalUsers);// 实时客流map
					ForcastUserTotalMap.put(substringHour, "-");// 预测客流map
					String stringHour = substringHour + ":00";
					userTime.add(stringHour);// 时间
					maximumList.add(substringHour);
				}
			}
		}
		Collections.sort(maximumList);
		if (maximumList.size() > 0) {
			maximum = maximumList.get(maximumList.size() - 1);
		} else {
			maximum = hour;
		}
		int maximumInt = Integer.parseInt(maximum);
		for (int i = 0; i < timeString.size(); i++) {
			String key = timeString.get(i);
			int keyInt = Integer.parseInt(key);
			if (userTotalMap.containsKey(key)) {
				continue;
			} else {
				if (keyInt <= maximumInt) {
					userTotalMap.put(key, "0");
				}
			}
		}
		String realPassengerNow = null;// 当前时间实时客流人数
		String realPassengerBefore = null;// 上一小时的实时客流人数
		// Integer beforeHour = Integer.parseInt(hour) - 1;
		Integer beforeHour = Integer.parseInt(maximum) - 1;
		String beforeString = null;
		String nextHour = null;
		if(timeType.equals("分钟")){
			String beforeFifiteen = null;
			String nextFifteen = null;
			String substringMinute = maximum.substring(2,4);
			String substringHour = maximum.substring(0,2);
			int parseIntMinute = Integer.parseInt(substringMinute);
			int parseIntHour = Integer.parseInt(substringHour);
			int beforeFifteenMinute = 0;
			int beforeFifteenHour = 0;
			int nextFifteenMinute = 0;
			int nextFifteenHour = 0;
			if(parseIntMinute==0){
				beforeFifteenMinute = 45;
				nextFifteenMinute = parseIntMinute + 15;
				if(parseIntHour==0){
					beforeFifteenHour = 23;
				}else{
					beforeFifteenHour = parseIntHour - 1;
				}
				nextFifteenHour = parseIntHour;
			}else{
				beforeFifteenMinute = parseIntMinute-15;
				nextFifteenMinute = parseIntMinute + 15;
				if(beforeFifteenMinute<0){
					beforeFifteenHour = parseIntHour - 1;
				}else{
					beforeFifteenHour = parseIntHour;
				}
				if(nextFifteenMinute==60){
					nextFifteenHour = parseIntHour + 1;
				}else{
					nextFifteenHour = parseIntHour;
				}
				if(nextFifteenHour==24){
					nextFifteenHour = 0;
				}
			}
			if(parseIntHour<10){
				if(beforeFifteenMinute==0){
					beforeFifiteen ="0" + String.valueOf(beforeFifteenHour) + beforeFifteenMinute + "0";
				}else if(parseIntHour==0&&beforeFifteenHour!=0){
					beforeFifiteen =String.valueOf(beforeFifteenHour) + beforeFifteenMinute;
				}else{
					beforeFifiteen ="0" + String.valueOf(beforeFifteenHour) + beforeFifteenMinute;
				}
			}else if(parseIntHour==10&&beforeFifteenHour<10){
				beforeFifiteen ="0" + String.valueOf(beforeFifteenHour) + beforeFifteenMinute;
			}else if(beforeFifteenMinute==0){
				beforeFifiteen = String.valueOf(beforeFifteenHour) + beforeFifteenMinute + "0";
			}else{
				beforeFifiteen = String.valueOf(beforeFifteenHour) + beforeFifteenMinute;
			}
			if(nextFifteenHour<10){
				if(nextFifteenMinute==60){
					nextFifteenMinute = 0;
					nextFifteen ="0" + String.valueOf(nextFifteenHour) + nextFifteenMinute + "0";
				}else{
					nextFifteen ="0" + String.valueOf(nextFifteenHour) + nextFifteenMinute;
				}
			}else{
				if(nextFifteenMinute==60){
					nextFifteenMinute = 0;
					nextFifteen = String.valueOf(nextFifteenHour) + nextFifteenMinute + "0";
				}else{
					nextFifteen = String.valueOf(nextFifteenHour) + nextFifteenMinute;
				}
			}
			beforeString = beforeFifiteen;
			nextHour = nextFifteen;
		}
		if(timeType.equals("小时")){
			if (beforeHour < 10) {
				beforeString = "0" + String.valueOf(beforeHour);// 上一小时
			} else {
				beforeString = String.valueOf(beforeHour);// 上一小时
			}
			// Integer hourToInt = Integer.parseInt(hour);
			Integer hourToInt = Integer.parseInt(maximum);
			hourToInt = hourToInt + 1;
			if (hourToInt < 10) {
				nextHour = "0" + String.valueOf(hourToInt);// 预测下一小时的时间
			} else {
				nextHour = String.valueOf(hourToInt);// 预测下一小时的时间
			}
			if (nextHour.equals("24")) {// 预测下一小时的时间若为24点，则改为00
				nextHour = "00";
			}
			if (hour.equals("00")) {
				nextHour = "0" + Integer.valueOf(hourToInt);// 当前时间若为00点，则下一小时为01
			}
		}
		String nextPassengerUc = null;
		if (userTotalMap.size() > 0 && formatTime.equals(beginTime)) {
			// if(userTotalMap.containsKey(hour)){
			// realPassengerNow = userTotalMap.get(hour);//当前实时客流人数
			realPassengerNow = userTotalMap.get(maximum);// 当前实时客流人数
			realPassengerBefore = userTotalMap.get(beforeString);// 前一小时实时客流人数
			if(ToolUtil.isNotEmpty(subAreaId)){
				if(timeType.equals("分钟")){
					BdapSubareaPopPropQm subAreaQmBefore = new BdapSubareaPopPropQm();
					BdapSubareaPopPropQm subAreaQmNow = new BdapSubareaPopPropQm();
					subAreaQmBefore.setTotalUsers(realPassengerBefore);
					subAreaQmNow.setTotalUsers(realPassengerNow);
					nextPassengerUc = PassengerFlowUtil.getSubAreaNextFifteenUc(subAreaQmNow, subAreaQmBefore);
				}else{
					BdapSubareaPopPropHm subAreaHmBefore = new BdapSubareaPopPropHm();
					BdapSubareaPopPropHm subAreaHmNow = new BdapSubareaPopPropHm();
					subAreaHmBefore.setTotalUsers(realPassengerBefore);
					subAreaHmNow.setTotalUsers(realPassengerNow);
					nextPassengerUc = PassengerFlowUtil.getSubAreaNextHourUc(subAreaHmNow, subAreaHmBefore);
				}
			}else{
				if(timeType.equals("分钟")){
					BdapAreaPopPropQm areaQmBefore = new BdapAreaPopPropQm();
					BdapAreaPopPropQm areaQmNow = new BdapAreaPopPropQm();
					areaQmBefore.setTotalUsers(realPassengerBefore);
					areaQmNow.setTotalUsers(realPassengerNow);
					nextPassengerUc = PassengerFlowUtil.getAreaNextFifteenUc(areaQmNow, areaQmBefore);
				}else{
					BdapAreaPopPropHm before = new BdapAreaPopPropHm();
					BdapAreaPopPropHm now = new BdapAreaPopPropHm();
					before.setTotalUsers(realPassengerBefore);
					now.setTotalUsers(realPassengerNow);
					nextPassengerUc = PassengerFlowUtil.getAreaNextHourUc(now, before);
				}
			}
			// ForcastUserTotalMap.put(hour,
			// realPassengerNow);//将当前时间客流添加进预测客流map中
			ForcastUserTotalMap.put(maximum, realPassengerNow);// 将当前时间客流添加进预测客流map中
			ForcastUserTotalMap.put(nextHour, nextPassengerUc);// 将预测的下一小时客流人数添加进预测客流map中
//			ForcastUserTotalMap.put(nextHour, nextHourUc);// 将预测的下一小时客流人数添加进预测客流map中
			if(timeType.equals("小时")){
				nextHour = nextHour + ":00";
			}else{
				String substringMinute = nextHour.substring(2,4);
				String substringHour = nextHour.substring(0,2);
				nextHour = substringHour + ":" +substringMinute;
			}
			for (int j = 0; j < timeString.size(); j++) {
				String str = timeString.get(j);
				String t = null;
				if(timeType.equals("小时")){
					t = str + ":00";
				}else{
					String substringMinute = str.substring(2,4);
					String substringHour = str.substring(0,2);
					t = substringHour + ":" +substringMinute;
				}
				userTime.add(t);
				// if(hour.equals(str)){
				if (maximum.equals(str)) {
					break;
				}
			}
			if (formatTime.equals(beginTime) && userTime.size() > 0) {
				userTime.add(nextHour);// 如果查询的是当天时间,则将预测时间添加进时间集合（折线图x轴）
			}
		}
		List<String> userTotalMapKey = new ArrayList<>();
		// 遍历实时客流map的key值，添加进集合中
		for (Entry<String, String> entry : userTotalMap.entrySet()) {
			String key = entry.getKey();
			userTotalMapKey.add(key);
		}
		// 对实时客流map的key值（时间）进行排序,变成00,01,02……，添加进集合中
		Collections.sort(userTotalMapKey);
		// 遍历排序后的集合，如果查询的是当天时间的实时客流，则将实时客流人数添加到realPassenger的实时客流集合中
		for (int i = 0; i < userTotalMapKey.size(); i++) {
			String key = userTotalMapKey.get(i);
			String value = userTotalMap.get(key);
			if (formatTime.equals(beginTime)) {
				realPassenger.getRealAllPerson().add(value);
				// hour：当前时间（小时）
				// if(hour.equals(key)){
				if (maximum.equals(key)) {
					realPassenger.getRealAllPerson().add("-");
					break;
				}
				ForcastUserTotalMap.put(key, "-");
			} else {
				realPassenger.getRealAllPerson().add(value);
			}
		}
		List<String> ForcastMapKey = new ArrayList<>();
		// 遍历预测客流map的key值，添加进集合中
		for (Entry<String, String> entry : ForcastUserTotalMap.entrySet()) {
			String key = entry.getKey();
			ForcastMapKey.add(key);
		}
		// 对预测客流map的key值（时间）进行排序,变成00,01,02……，添加进集合中
		Collections.sort(ForcastMapKey);
		// 遍历排序后的集合，如果查询的是当天时间的实时客流，则将预测客流人数添加到realPassenger的预测客流集合中
		for (int i = 0; i < ForcastMapKey.size(); i++) {
			String key = ForcastMapKey.get(i);
			String value = ForcastUserTotalMap.get(key);
			if (formatTime.equals(beginTime)) {
				realPassenger.getRealFluidPerson().add(value);
			}
		}
		for (String str : userTime) {
			realPassenger.getRealPassengerTimes().add(str);
			if (nextHour.equals(str) && formatTime.equals(beginTime)) {
				break;
			}
		}
		return realPassenger;
	}

	/**
	 * 加载累计客流图表数据
	 * 
	 * @param beginTime
	 * @param upTime
	 * @param timeType
	 *            (日，月）
	 * @return
	 */
	@RequestMapping(value = "/cumulativePassenger")
	@ResponseBody
	public Object cumulativePassenger(String beginTime, @RequestParam(required = false) String upTime, String timeType,
			String areaId, String subAreaId) {
		if (ToolUtil.isEmpty(beginTime) || ToolUtil.isEmpty(timeType)) {
			return null;
		}
		CumulativePassenger cumulativePassenger = new CumulativePassenger();
		MyTimeType mytimeType = new MyTimeType();
		mytimeType.setAreaId(areaId);
		mytimeType.setSubAreaId(subAreaId);
		if (timeType.equals("日")) {
			mytimeType.setBeginTime(beginTime);
			mytimeType.setUpTime(upTime);
			if(ToolUtil.isNotEmpty(subAreaId)){
				List<SubAreaDmModel> subAreaDmList = subAreaDmMapper.selectSubAreaCumulativePassenger(mytimeType);
				for (SubAreaDmModel bdapSubareaPopPropDm : subAreaDmList) {
					String comulativeAllPerson = bdapSubareaPopPropDm.getComulativeAllPerson();
					String comulativeFluidPerson = bdapSubareaPopPropDm.getComulativeFluidPerson();
					cumulativePassenger.getComulativeAllPerson().add(comulativeAllPerson);
					cumulativePassenger.getComulativeFluidPerson().add(comulativeFluidPerson);
					cumulativePassenger.getCumulativePassengerTimes().add(bdapSubareaPopPropDm.getStatDate());
				}
			}else{
				List<AreaDmModel> areaDmList = areaDmMapper.selectAreaCumulativePassenger(mytimeType);
				for (AreaDmModel bdapAreaPopPropDm : areaDmList) {
					String comulativeAllPerson = bdapAreaPopPropDm.getComulativeAllPerson();
					String comulativeFluidPerson = bdapAreaPopPropDm.getComulativeFluidPerson();
					cumulativePassenger.getComulativeAllPerson().add(comulativeAllPerson);
					cumulativePassenger.getComulativeFluidPerson().add(comulativeFluidPerson);
					cumulativePassenger.getCumulativePassengerTimes().add(bdapAreaPopPropDm.getStatDate());
				}
			}
		}
		if (timeType.equals("月")) {
			List<String> timeList = TimeUtil.getLastDayOfMonth(beginTime);
			mytimeType.setBeginTime(StringUtil.concat(timeList));
			if(ToolUtil.isNotEmpty(subAreaId)){
				List<SubAreaDtModel> subAreaDtList = subAreaDtMapper.selectSubAreaCumulative(mytimeType);
				for (SubAreaDtModel bdapSubareaPopPropDt : subAreaDtList) {
					String comulativeAllPerson = bdapSubareaPopPropDt.getComulativeAllPerson();
					String comulativeFluidPerson = bdapSubareaPopPropDt.getComulativeFluidPerson();
					cumulativePassenger.getComulativeAllPerson().add(comulativeAllPerson);
					cumulativePassenger.getComulativeFluidPerson().add(comulativeFluidPerson);
					cumulativePassenger.getCumulativePassengerTimes().add(bdapSubareaPopPropDt.getStatDate());
				}
			}else{
				List<AreaDtModel> areaDtList = areaDtMapper.selectAreaCumulative(mytimeType);
				for (AreaDtModel bdapAreaPopPropDt : areaDtList) {
					String comulativeAllPerson = bdapAreaPopPropDt.getComulativeAllPerson();
					String comulativeFluidPerson = bdapAreaPopPropDt.getComulativeFluidPerson();
					cumulativePassenger.getComulativeAllPerson().add(comulativeAllPerson);
					cumulativePassenger.getComulativeFluidPerson().add(comulativeFluidPerson);
					cumulativePassenger.getCumulativePassengerTimes().add(bdapAreaPopPropDt.getStatDate());
				}
			}
		}
		return cumulativePassenger;
	}

	/**
	 * 加载峰值客流数据
	 * 
	 * @Description: TODO
	 * @param @param
	 *            beginTime
	 * @param @param
	 *            upTime
	 * @param @param
	 *            timeType
	 * @param @return
	 * @return Object
	 * @throws @author
	 *             zhangyaoyao
	 * @date 2017年11月21日
	 */
	@RequestMapping(value = "/peakPopulation")
	@ResponseBody
	public Object peakPassengerFind(String beginTime, @RequestParam(required = false) String upTime, String timeType,
			String areaId, String subAreaId) {
		if (ToolUtil.isEmpty(beginTime) || ToolUtil.isEmpty(timeType)) {
			return null;
		}
		PeakPassenger peakPassenger = new PeakPassenger();
		MyTimeType mytimeType = new MyTimeType();
		mytimeType.setAreaId(areaId);
		mytimeType.setSubAreaId(subAreaId);

		if (ToolUtil.isEmpty(subAreaId)) {
			BdapArea area = areaMapper.selectById(Long.parseLong(areaId));
			if (ToolUtil.isEmpty(area)) {
				return null;
			}
			peakPassenger.getPeakWarningValue().add(area.getWarningValue());
			peakPassenger.getPeakSaturationValue().add(area.getSaturationValue());
			peakPassenger.getPeakServerValue().add(area.getSevereValue());
		} else {
			BdapSubArea subarea = subareaMapper.selectById(Long.parseLong(subAreaId));
			if (ToolUtil.isEmpty(subarea)) {
				return null;
			}
			peakPassenger.getPeakWarningValue().add(subarea.getWarningValue());
			peakPassenger.getPeakSaturationValue().add(subarea.getSaturationValue());
			peakPassenger.getPeakServerValue().add(subarea.getSevereValue());
		}
		if (timeType.equals("日")) {
			mytimeType.setBeginTime(beginTime);
			mytimeType.setUpTime(upTime);
			if(ToolUtil.isNotEmpty(subAreaId)){
				List<SubAreaHmModel> subAreaDmList = subAreaHmMapper.selectsubAreaDmPeakPassenge(mytimeType);
				for (SubAreaHmModel subAreaHmModel : subAreaDmList) {
					peakPassenger.getPeakAllPerson().add(subAreaHmModel.getPeakAllPerson());
					peakPassenger.getPeakFluidPerson().add(subAreaHmModel.getPeakFluidPerson());
					peakPassenger.getPeakPassengerTimes().add(subAreaHmModel.getStatDate());
				}
			}else{
				List<AreaHmModel> areaDmList = areaHmMapper.selectAreaDmPeakPassenge(mytimeType);
				for (AreaHmModel areaHmModel : areaDmList) {
					peakPassenger.getPeakAllPerson().add(areaHmModel.getPeakAllPerson());
					peakPassenger.getPeakFluidPerson().add(areaHmModel.getPeakFluidPerson());
					peakPassenger.getPeakPassengerTimes().add(areaHmModel.getStatDate());
				}
			}
		}
		if (timeType.equals("月")) {
			// 每月最后一天日期
			mytimeType.setBeginTime(beginTime + "01");
			mytimeType.setUpTime(beginTime + "12");
			if(ToolUtil.isNotEmpty(subAreaId)){
				List<SubAreaDmModel> subAreaDmList = subAreaDmMapper.selectSubAreaPeakPassengerByMon(mytimeType);
				for (SubAreaDmModel bdapSubareaPopPropDm : subAreaDmList) {
					peakPassenger.getPeakAllPerson().add(bdapSubareaPopPropDm.getPeakAllPerson());
					peakPassenger.getPeakFluidPerson().add(bdapSubareaPopPropDm.getPeakFluidPerson());
					peakPassenger.getPeakPassengerTimes().add(bdapSubareaPopPropDm.getStatMonth());
				}
			}else{
				List<AreaDmModel> areaDmList = areaDmMapper.selectAreaPeakPassengerByMon(mytimeType);
				for (AreaDmModel bdapAreaPopPropDm : areaDmList) {
					peakPassenger.getPeakAllPerson().add(bdapAreaPopPropDm.getPeakAllPerson());
					peakPassenger.getPeakFluidPerson().add(bdapAreaPopPropDm.getPeakFluidPerson());
					peakPassenger.getPeakPassengerTimes().add(bdapAreaPopPropDm.getStatMonth());
				}
			}
		}
		return peakPassenger;
	}

	/**
	 * 加载常驻人口图表数据
	 * 
	 * @param beginTime
	 *            时间
	 * @return
	 */
	@RequestMapping(value = "/permanent")
	@ResponseBody
	public Object permanent(String beginTime, String areaId, String subAreaId) {
		if (ToolUtil.isEmpty(beginTime)) {
			return null;
		}
		PermanentPeople permanentPeople = new PermanentPeople();
		MyTimeType mytimeType = new MyTimeType();
		mytimeType.setAreaId(areaId);
		mytimeType.setSubAreaId(subAreaId);
		// 每月最后一天日期
		List<String> timeList = TimeUtil.getLastDayOfMonth(beginTime);
		mytimeType.setBeginTime(StringUtil.concat(timeList));
		if(ToolUtil.isNotEmpty(subAreaId)){
			List<SubAreaDtModel> subAreaPermanentPeople = subAreaDtMapper.seletcSubAreaPermanentPeople(mytimeType);
			for (SubAreaDtModel subAreaDtModel : subAreaPermanentPeople) {
				permanentPeople.getPermanentPeople().add(subAreaDtModel.getStayLocalUsers());
				permanentPeople.getPermanentPeopleTimes().add(subAreaDtModel.getStatDate());
			}
		}else{
			List<AreaDtModel> areaPermanentPeople = areaDtMapper.seletcAreaPermanentPeople(mytimeType);
			for (AreaDtModel areaDtModel : areaPermanentPeople) {
				permanentPeople.getPermanentPeople().add(areaDtModel.getStayLocalUsers());
				permanentPeople.getPermanentPeopleTimes().add(areaDtModel.getStatDate());
			}
		}
		return permanentPeople;
	}

	/**
	 * 加载工作人口图表数据
	 * 
	 * @param beginTime
	 *            时间
	 * @return
	 */
	@RequestMapping(value = "/workForce")
	@ResponseBody
	public Object workForce(String beginTime, String areaId, String subAreaId) {
		if (ToolUtil.isEmpty(beginTime)) {
			return null;
		}
		WorkForce workForce = new WorkForce();
		MyTimeType mytimeType = new MyTimeType();
		mytimeType.setAreaId(areaId);
		mytimeType.setSubAreaId(subAreaId);
		// 每月最后一天日期
		List<String> timeList = TimeUtil.getLastDayOfMonth(beginTime);
		mytimeType.setBeginTime(StringUtil.concat(timeList));
		// 查找最近一年的所有月份数据
		if(ToolUtil.isNotEmpty(subAreaId)){
			List<SubAreaDtModel> seletcSubAreaWorkingPopulation = subAreaDtMapper.seletcSubAreaWorkingPopulation(mytimeType);
			for (SubAreaDtModel subAreaDtModel : seletcSubAreaWorkingPopulation) {
				workForce.getWorkingPopulation().add(subAreaDtModel.getWorkLocalUsers());
				workForce.getWorkingPopulationTimes().add(subAreaDtModel.getStatDate());
			}
		}else{
			List<AreaDtModel> AreaWorkingPopulation = areaDtMapper.seletcAreaWorkingPopulation(mytimeType);
			for (AreaDtModel areaDtModel : AreaWorkingPopulation) {
				workForce.getWorkingPopulation().add(areaDtModel.getWorkLocalUsers());
				workForce.getWorkingPopulationTimes().add(areaDtModel.getStatDate());
			}
		}
		return workForce;
	}

	/**
	 * 加载途经人口图表数据
	 * 
	 * @param beginTime
	 *            时间
	 * @return
	 */
	@RequestMapping(value = "/passingPopulation")
	@ResponseBody
	public Object passingPopulation(String beginTime, String upTime, String timeType, String areaId, String subAreaId) {
		if (ToolUtil.isEmpty(beginTime) || ToolUtil.isEmpty(timeType)) {
			return null;
		}
		PassingPopulation passingPopulation = new PassingPopulation();
		MyTimeType mytimeType = new MyTimeType();
		mytimeType.setAreaId(areaId);
		mytimeType.setSubAreaId(subAreaId);
		if (timeType.equals("天")) {
			mytimeType.setBeginTime(beginTime);
			mytimeType.setUpTime(upTime);
			if(ToolUtil.isNotEmpty(subAreaId)){
				List<SubAreaDmModel> subAreaCumulativePassenger = subAreaDmMapper.selectSubAreaCumulativePassenger(mytimeType);
				for (SubAreaDmModel subAreaDmModel : subAreaCumulativePassenger) {
					String passingPerson = subAreaDmModel.getComulativeFluidPerson();
					passingPopulation.getPassingPopulation().add(passingPerson);
					passingPopulation.getPassingPopulationTimes().add(subAreaDmModel.getStatDate());
				}
			}else{
				List<AreaDmModel> areaCumulativePassenger = areaDmMapper.selectAreaCumulativePassenger(mytimeType);
				for (AreaDmModel areaDmModel : areaCumulativePassenger) {
					String passingPerson = areaDmModel.getComulativeFluidPerson();
					passingPopulation.getPassingPopulation().add(passingPerson);
					passingPopulation.getPassingPopulationTimes().add(areaDmModel.getStatDate());
				}
			}
		}
		if (timeType.equals("小时")) {
			mytimeType.setBeginTime(beginTime);
			mytimeType.setUpTime(beginTime);
			if(ToolUtil.isNotEmpty(subAreaId)){
				List<SubAreaHmModel> selectSubAreaPopulation = subAreaHmMapper.selectSubAreaPopulation(mytimeType);
				for (SubAreaHmModel subAreaHmModel : selectSubAreaPopulation) {
					passingPopulation.getPassingPopulation().add(subAreaHmModel.getPassingPerson());
					passingPopulation.getPassingPopulationTimes().add(subAreaHmModel.getStatHour());
				}
			}else{
				List<AreaHmModel> selectAreaPassingPerson = areaHmMapper.selectAreaPassingPerson(mytimeType);
				for (AreaHmModel areaHmModel : selectAreaPassingPerson) {
					passingPopulation.getPassingPopulation().add(areaHmModel.getPassingPerson());
					passingPopulation.getPassingPopulationTimes().add(areaHmModel.getStatHour());
				}
			}
		}
		return passingPopulation;
	}

	/**
	 * 加载出行人口图表数据
	 * 
	 * @param beginTime
	 *            时间
	 * @return
	 */
	@RequestMapping(value = "/travelPopulation")
	@ResponseBody
	public Object travelPopulation(String beginTime, @RequestParam(required = false) String upTime, String timeType,
			String areaId, String subAreaId) {
		if (ToolUtil.isEmpty(beginTime) || ToolUtil.isEmpty(timeType)) {
			return null;
		}
		TravelPopulation travelPopulation = new TravelPopulation();
		MyTimeType mytimeType = new MyTimeType();
		mytimeType.setAreaId(areaId);
		mytimeType.setSubAreaId(subAreaId);
		mytimeType.setBeginTime(TimeUtil.getMonth(beginTime.substring(0, 6), 2));
		TraceMonth traceMonth = traceMonthMapper.selectPermanentPeople(mytimeType);
		if (ToolUtil.isEmpty(traceMonth)) {
			return null;
		}
		BigDecimal permanent = traceMonth.getPermanentPeople();
		if (ToolUtil.isEmpty(permanent) || permanent.intValue() == 0) {
			return null;
		}
		if (timeType.equals("天")) {
			mytimeType.setBeginTime(beginTime);
			mytimeType.setUpTime(upTime);
			if(ToolUtil.isNotEmpty(subAreaId)){
				List<SubAreaDmModel> subAreaPermanentPeopleDm = subAreaDmMapper.selectSubAreaPermanentPeopleDm(mytimeType);
				for (SubAreaDmModel subAreaDmModel : subAreaPermanentPeopleDm) {
					String permanentPeople = subAreaDmModel.getPermanentPeople();
					Integer valueOf = Integer.valueOf(permanentPeople);
					if (permanent.intValue() - valueOf < 0) {
						travelPopulation.getTravelPopulation().add(0);
					} else {
						travelPopulation.getTravelPopulation().add(permanent.intValue() - valueOf);
					}
					travelPopulation.getTravelPopulationTimes().add(subAreaDmModel.getStatDate());
				}
			}else{
				List<AreaDmModel> areaPermanentPeopleDm = areaDmMapper.selectAreaPermanentPeopleDm(mytimeType);
				for (AreaDmModel areaDmModel : areaPermanentPeopleDm) {
					String permanentPeople = areaDmModel.getPermanentPeople();
					Integer valueOf = Integer.valueOf(permanentPeople);
					if (permanent.intValue() - valueOf < 0) {
						travelPopulation.getTravelPopulation().add(0);
					} else {
						travelPopulation.getTravelPopulation().add(permanent.intValue() - valueOf);
					}
					travelPopulation.getTravelPopulationTimes().add(areaDmModel.getStatDate());
				}
			}
		}
		if (timeType.equals("小时")) {
			mytimeType.setBeginTime(beginTime);
			mytimeType.setUpTime(beginTime);
			if(ToolUtil.isNotEmpty(subAreaId)){
				List<SubAreaHmModel> subAreaPopulation = subAreaHmMapper.selectSubAreaPopulation(mytimeType);
				for (SubAreaHmModel subAreaHmModel : subAreaPopulation) {
					String permanentPeople = subAreaHmModel.getPermanentPeople();
					Integer valueOf = Integer.valueOf(permanentPeople);
					if (permanent.intValue() - valueOf < 0) {
						travelPopulation.getTravelPopulation().add(0);
					} else {
						travelPopulation.getTravelPopulation().add(permanent.intValue() - valueOf);
					}
					travelPopulation.getTravelPopulationTimes().add(subAreaHmModel.getStatHour());
				}
			}else{
				List<AreaHmModel> areaPassingPerson = areaHmMapper.selectAreaPassingPerson(mytimeType);
				for (AreaHmModel areaHmModel : areaPassingPerson) {
					String permanentPeople = areaHmModel.getPermanentPeople();
					Integer valueOf = Integer.valueOf(permanentPeople);
					if (permanent.intValue() - valueOf < 0) {
						travelPopulation.getTravelPopulation().add(0);
					} else {
						travelPopulation.getTravelPopulation().add(permanent.intValue() - valueOf);
					}
					travelPopulation.getTravelPopulationTimes().add(areaHmModel.getStatHour());
				}
			}
		}
		return travelPopulation;
	}

	/**
	 * 客流密度
	 * 
	 * @Description: TODO
	 * @param @param
	 *            beginTime
	 * @param @param
	 *            upTime
	 * @param @param
	 *            timeType
	 * @param @return
	 * @return Object
	 * @throws @author
	 *             zhangyaoyao
	 * @date 2017年11月26日
	 */
	@RequestMapping(value = "/passengerDensity")
	@ResponseBody
	public Object passengerDensity(String beginTime, @RequestParam(required = false) String upTime, String timeType,
			String areaId, String subAreaId) {
		String name = null;
		if (ToolUtil.isEmpty(beginTime) || ToolUtil.isEmpty(timeType)) {
			return null;
		}
		int saturationValue = 0;
		if (ToolUtil.isEmpty(subAreaId)) {
			BdapArea area = areaMapper.selectById(Long.parseLong(areaId));
			if (ToolUtil.isEmpty(area)) {
				return null;
			}
			saturationValue = area.getSaturationValue();
			name = area.getName();
		} else {
			BdapSubArea subarea = subareaMapper.selectById(Long.parseLong(subAreaId));
			if (ToolUtil.isEmpty(subarea)) {
				return null;
			}
			saturationValue = subarea.getSaturationValue();
			name = subarea.getName();
		}
		PassingDensity passingDensity = new PassingDensity();
		MyTimeType mytimeType = new MyTimeType();
		mytimeType.setAreaId(areaId);
		mytimeType.setSubAreaId(subAreaId);
		if (timeType.equals("月")) {
			mytimeType.setUpTime(upTime);
		}
		if (timeType.equals("天")) {
			mytimeType.setUpTime(beginTime);
		}
		mytimeType.setBeginTime(beginTime);
		if(ToolUtil.isNotEmpty(subAreaId)){
			List<SubAreaDmModel> subAreaCumulativePassenger = subAreaDmMapper.selectSubAreaCumulativePassenger(mytimeType);
			for (SubAreaDmModel subAreaDmModel : subAreaCumulativePassenger) {
				String comulativeAllPerson = subAreaDmModel.getComulativeAllPerson();
				String statDate = subAreaDmModel.getStatDate();
				if (saturationValue == 0) {
					passingDensity.getPassingDensity().add("0");
					passingDensity.getPassingDensityPercent().add("0");
				} else {
					passingDensity.getPassingDensityTime().add(statDate);
					passingDensity.getPassingDensity().add(comulativeAllPerson);
					passingDensity.getPassingDensityPercent()
							.add(AreaUtil.getPassengerDensity(saturationValue, Integer.parseInt(comulativeAllPerson)));
				}
			}
		}else{
			List<AreaDmModel> areaCumulativePassenger = areaDmMapper.selectAreaCumulativePassenger(mytimeType);
			for (AreaDmModel areaDmModel : areaCumulativePassenger) {
				String comulativeAllPerson = areaDmModel.getComulativeAllPerson();
				String statDate = areaDmModel.getStatDate();
				if (saturationValue == 0) {
					passingDensity.getPassingDensity().add("0");
					passingDensity.getPassingDensityPercent().add("0");
				} else {
					passingDensity.getPassingDensityTime().add(statDate);
					passingDensity.getPassingDensity().add(comulativeAllPerson);
					passingDensity.getPassingDensityPercent()
							.add(AreaUtil.getPassengerDensity(saturationValue, Integer.parseInt(comulativeAllPerson)));
				}
			}
		}
		passingDensity.setName(name);
		return passingDensity;
	}

	/**
	 * 加载性别分布图表数据
	 * 
	 * @param beginTime
	 *            时间
	 * @return
	 */
	@RequestMapping(value = "/sex")
	@ResponseBody
	public Object sex(String beginTime, String upTime, String timeType, String areaId, String subAreaId) {
		if (ToolUtil.isEmpty(upTime) || ToolUtil.isEmpty(beginTime)
				|| ToolUtil.isEmpty(timeType)) {
			return null;
		}
		Sex sex = new Sex();
		Map<String, Integer> sexBeginMonthMap = new LinkedHashMap<String, Integer>();// 求和后的map（月份）
		Map<String, Integer> sexUpMap = new LinkedHashMap<String, Integer>();// 求和后的map（月份）
		Map<String, ArrayList<Integer>> allSexMap = new LinkedHashMap<String, ArrayList<Integer>>();// 对两个时间段的求和数据汇总
		Set<String> sexTimeList = new LinkedHashSet<String>();// 存放时间集合
		List<String> sexSegmentation = new ArrayList<String>();// 存放性别字段
		List<String> selectSexList = new ArrayList<String>();// 存放解析集合
		List<String> sexListBeginMonth = new ArrayList<String>();// 存放解析集合(月份）
		List<String> sexListUpMonth = new ArrayList<String>();// 存放解析集合（月份）
		List<Integer> sumListSex = new ArrayList<Integer>();// 求和后的list
		List<Integer> sumListBeginTime = new ArrayList<Integer>();// 求和后的list(月份）
		List<Integer> sumListUptime = new ArrayList<Integer>();// 求和后的list(月份）
		Map<String, List<Integer>> splitToMap = new LinkedHashMap<>();
		Map<String, List<Integer>> splitToMapMonBegin = new LinkedHashMap<>();// 月份
		Map<String, List<Integer>> splitToMapMonUp = new LinkedHashMap<>();
		Map<String, ArrayList<String>> map = new LinkedHashMap<String, ArrayList<String>>();// 存放根据时间查询处理的结果集
		Map<String, Map<String, List<Integer>>> convertMap = new LinkedHashMap<String, Map<String, List<Integer>>>();// 存放根据时间拆分后的map
		List<String> timeKey = new ArrayList<String>();// 存放时间key
		MyTimeType mytimeType = new MyTimeType();
		mytimeType.setAreaId(areaId);
		mytimeType.setSubAreaId(subAreaId);
		if (StringUtils.isNotEmpty(timeType)) {
			if (timeType.equals("日")) {
				if (StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(upTime)) {
					mytimeType.setBeginTime(beginTime);
					mytimeType.setUpTime(upTime);
					// 1.根据时间查询出性别数据
					// 2.根据拆分方法splitUtil.splitToMap将数据拆分成Map<String,
					// Integer[]>形式（0-9，[20,30……]、10-19，[60,300……]……)
					if(ToolUtil.isNotEmpty(subAreaId)){
						List<SubAreaDmModel> subAreaSex = subAreaDmMapper.selectSubAreaSexByTime(mytimeType);
						for (SubAreaDmModel subAreaDmModel : subAreaSex) {
							String sexUsers = subAreaDmModel.getSexUsers();
							selectSexList.add(sexUsers);
							String time = subAreaDmModel.getStatDate();
							if (map.containsKey(time)) {
								ArrayList<String> arrayList = map.get(time);
								arrayList.add(sexUsers);
							} else {
								ArrayList<String> arrayList = new ArrayList<String>();
								arrayList.add(sexUsers);
								map.put(time, arrayList);
							}
						}
					}else{
						List<AreaDmModel> areaSex = areaDmMapper.selectAreaSexByTime(mytimeType);
						for (AreaDmModel areaDmModel : areaSex) {
							String sexUsers = areaDmModel.getSexUsers();
							selectSexList.add(sexUsers);
							String time = areaDmModel.getStatDate();
							if (map.containsKey(time)) {
								ArrayList<String> arrayList = map.get(time);
								arrayList.add(sexUsers);
							} else {
								ArrayList<String> arrayList = new ArrayList<String>();
								arrayList.add(sexUsers);
								map.put(time, arrayList);
							}
						}
					}
					for (Entry<String, ArrayList<String>> entry : map.entrySet()) {
						String key = entry.getKey();
						timeKey.add(key);
						ArrayList<String> arrayList = map.get(key);
						Map<String, List<Integer>> hashMap = MySplitUtilToList.splitToMap(arrayList);
						convertMap.put(key, hashMap);
					}
					Collections.sort(timeKey);
					splitToMap = MySplitUtilToList.splitToMap(selectSexList);
				}
			}

			if (timeType.equals("月")) {
				if (StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(upTime)) {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
					DateFormat fmtDateTime = new SimpleDateFormat("yyyyMMdd");
					Date date = null;
					try {
						date = fmtDateTime.parse(beginTime);
					} catch (ParseException e) {
						e.printStackTrace();
					}
					String firstday = null;
					String lastday = null;
					Calendar cale = Calendar.getInstance();
					cale.setTime(date);
					// 获取beginTime的最后一天
					cale.add(Calendar.MONTH, 1);
					cale.set(Calendar.DAY_OF_MONTH, 0);
					lastday = sdf.format(cale.getTime());
					mytimeType.setBeginTime(beginTime);
					mytimeType.setUpTime(lastday);
					List<SubAreaDmModel> subAreaSex = new ArrayList<SubAreaDmModel>();
					List<AreaDmModel> areaSex = new ArrayList<AreaDmModel>();
					if(ToolUtil.isNotEmpty(subAreaId)){
						subAreaSex = subAreaDmMapper.selectSubAreaSexByTime(mytimeType);
						for (SubAreaDmModel subAreaDmModel : subAreaSex) {
							String sexUsers = subAreaDmModel.getSexUsers();
							sexListBeginMonth.add(sexUsers);
						}
					}else{
						areaSex = areaDmMapper.selectAreaSexByTime(mytimeType);
						for (AreaDmModel areaDmModel : areaSex) {
							String sexUsers = areaDmModel.getSexUsers();
							sexListBeginMonth.add(sexUsers);
						}
					}
					try {
						date = fmtDateTime.parse(upTime);
					} catch (ParseException e) {
						e.printStackTrace();
					}
					cale.setTime(date);
					cale.add(Calendar.MONTH, 0);
					cale.set(Calendar.DAY_OF_MONTH, 1);
					firstday = sdf.format(cale.getTime());
					mytimeType.setBeginTime(firstday);
					mytimeType.setUpTime(upTime);
					List<SubAreaDmModel> subAreaSexUpTime = new ArrayList<SubAreaDmModel>();
					List<AreaDmModel> areaSexUpTime = new ArrayList<AreaDmModel>();
					if(ToolUtil.isNotEmpty(subAreaId)){
						subAreaSexUpTime = subAreaDmMapper.selectSubAreaSexByTime(mytimeType);
						for (SubAreaDmModel subAreaDmModel : subAreaSexUpTime) {
							String sexUsers = subAreaDmModel.getSexUsers();
							sexListUpMonth.add(sexUsers);
						}
					}else{
						areaSexUpTime = areaDmMapper.selectAreaSexByTime(mytimeType);
						for (AreaDmModel areaDmModel : areaSexUpTime) {
							String sexUsers = areaDmModel.getSexUsers();
							sexListUpMonth.add(sexUsers);
						}
					}
					mytimeType.setBeginTime(beginTime);
					mytimeType.setUpTime(upTime);
					// 1.根据时间查询出性别数据
					// 2.根据拆分方法splitUtil.splitToMap将数据拆分成Map<String,
					// Integer[]>形式（男，[20,30……]、女，[60,300……]……)
					// 3.将查询时间2017-9-20,2017-10-15格式化为2017-9，2017-10形式
					// 4.查询出2017-9月份的20号至9月最后一天的数据
					// 5.查询出2017-10月份的1号到15的数据
					// 6.根据时间2017-9-20,2017-10-15查询出数据（柱状图数据）
					// 5.将结果封装到对应的集合中

					// 根据时间查询（柱状图数据）
					if(ToolUtil.isNotEmpty(subAreaId)){
						List<SubAreaDmModel> subAreaSexDm = subAreaDmMapper.selectSubAreaSexByTime(mytimeType);
						for (SubAreaDmModel subAreaDmModel : subAreaSexDm) {
							String sexUsers = subAreaDmModel.getSexUsers();
							selectSexList.add(sexUsers);
						}
					}else{
						List<AreaDmModel> areaSexDm = areaDmMapper.selectAreaSexByTime(mytimeType);
						for (AreaDmModel areaDmModel : areaSexDm) {
							String sexUsers = areaDmModel.getSexUsers();
							selectSexList.add(sexUsers);
						}
					}
					splitToMap = MySplitUtilToList.splitToMap(selectSexList);
					String formatBeginTime = beginTime.substring(0, 6);
					String formatUpTime = upTime.substring(0, 6);
					if (subAreaSexUpTime.size() > 0 || areaSexUpTime.size() > 0 || subAreaSex.size() > 0 || areaSex.size() > 0) {
						sexTimeList.add(formatBeginTime);
						sexTimeList.add(formatUpTime);
					}
					splitToMapMonBegin = MySplitUtilToList.splitToMap(sexListBeginMonth);
					splitToMapMonUp = MySplitUtilToList.splitToMap(sexListUpMonth);
				}
			}
		}

		Map<String, List<Integer>> splitToMapList = new LinkedHashMap<String, List<Integer>>();
		for (int i = 0; i < timeKey.size(); i++) {
			String string = timeKey.get(i);// string：时间段20171106
			sexTimeList.add(string);
			Map<String, List<Integer>> hashMap = convertMap.get(string);
			for (Entry<String, List<Integer>> entry : hashMap.entrySet()) {
				String key = entry.getKey();// key：性别：男、女
				List<Integer> value = hashMap.get(key);
				Integer sum = 0;
				for (Integer integer : value) {
					sum = sum + integer;
				}
				List<Integer> list = new ArrayList<Integer>();
				list.add(sum);
				if (splitToMapList.containsKey(key)) {
					List<Integer> splitList = splitToMapList.get(key);
					splitList.add(sum);
				} else {
					List<Integer> arrayList = new ArrayList<Integer>();
					arrayList.add(sum);
					splitToMapList.put(key, list);
				}
			}
		}

		// 1.将拆分的数据Map<String,
		// Integer[]>形式（男，[20,30……]、女，[60,300……]……)的value值取出进行求和
		// 2.将key值和求和的值封装
		for (Entry<String, List<Integer>> entry : splitToMap.entrySet()) {
			String key = entry.getKey();
			List<Integer> value = entry.getValue();
			Integer sum = 0;
			for (Integer integer : value) {
				sum = sum + integer;
			}
			sexSegmentation.add(key);
			sumListSex.add(sum);
		}

		// 对相应的月份的值进行求和
		for (Entry<String, List<Integer>> entry : splitToMapMonUp.entrySet()) {
			String key = entry.getKey();
			List<Integer> value = entry.getValue();
			Integer sum = 0;
			for (Integer integer : value) {
				sum = sum + integer;
			}
			sumListBeginTime.add(sum);
			sexUpMap.put(key, sum);
		}

		// 对相应的月份的值进行求和
		for (Entry<String, List<Integer>> entry : splitToMapMonBegin.entrySet()) {
			String key = entry.getKey();
			List<Integer> value = entry.getValue();
			Integer sum = 0;
			for (Integer integer : value) {
				sum = sum + integer;
			}
			sumListUptime.add(sum);
			sexBeginMonthMap.put(key, sum);
		}

		// 根据Key值对相应的月份数据进行封装（按月查询）
		for (int i = 0; i < sexSegmentation.size(); i++) {
			String key = sexSegmentation.get(i);
			Integer sexBegin = sexBeginMonthMap.get(key);
			Integer sexUp = sexUpMap.get(key);
			List<Integer> list = new ArrayList<>();
			if (sexBegin == null) {
				list.add(0);
			} else {
				list.add(sexBegin);
			}
			if (sexUp == null) {
				list.add(0);
			} else {
				list.add(sexUp);
			}
			allSexMap.put(key, (ArrayList<Integer>) list);
		}
		sex.setSplitToMapList(splitToMapList);
		sex.setSexTimes(sexTimeList);
		sex.setSexSegmentation(sexSegmentation);
		sex.setSumSexList(sumListSex);
		sex.setSumListBeginTime(sumListBeginTime);
		sex.setSumListUptime(sumListUptime);
		sex.setAllSexMap(allSexMap);
		return sex;
	}

	/**
	 * 加载年龄分布图表数据
	 * 
	 * @param beginTime
	 *            时间
	 * @return
	 */
	@RequestMapping(value = "/age")
	@ResponseBody
	public Object age(String beginTime, String upTime, String timeType, String areaId, String subAreaId) {
		if (ToolUtil.isEmpty(areaId) || ToolUtil.isEmpty(upTime) || ToolUtil.isEmpty(beginTime)
				|| ToolUtil.isEmpty(timeType)) {
			return null;
		}
		AgeSegmentation age = new AgeSegmentation();
		Map<String, Integer> ageBeginMonthMap = new LinkedHashMap<String, Integer>();// 求和后的map（月份）
		Map<String, Integer> ageUpMap = new LinkedHashMap<String, Integer>();// 求和后的map（月份）
		Map<String, ArrayList<Integer>> allAgeMap = new LinkedHashMap<String, ArrayList<Integer>>();// 对两个时间段的求和数据汇总
		Set<String> ageTimeList = new LinkedHashSet<String>();// 存放时间集合
		List<String> ageSegmentation = new ArrayList<String>();// 存放年龄分段字段
		List<String> selectAgeList = new ArrayList<String>();// 存放解析集合
		List<String> ageListBeginMonth = new ArrayList<String>();// 存放解析集合(月份）
		List<String> ageListUpMonth = new ArrayList<String>();// 存放解析集合（月份）
		List<Integer> sumListAge = new ArrayList<Integer>();// 求和后的list
		List<Integer> sumListBeginTime = new ArrayList<Integer>();// 求和后的list(月份）
		List<Integer> sumListUptime = new ArrayList<Integer>();// 求和后的list(月份）
		Map<String, List<Integer>> splitToMap = new LinkedHashMap<>();
		Map<String, List<Integer>> splitToMapMonBegin = new LinkedHashMap<>();// 月份
		Map<String, List<Integer>> splitToMapMonUp = new LinkedHashMap<>();
		Map<String, ArrayList<String>> map = new LinkedHashMap<String, ArrayList<String>>();// 存放根据时间查询处理的结果集
		Map<String, Map<String, List<Integer>>> convertMap = new LinkedHashMap<String, Map<String, List<Integer>>>();// 存放根据时间拆分后的map
		List<String> timeKey = new ArrayList<String>();// 存放时间key
		MyTimeType mytimeType = new MyTimeType();
		mytimeType.setAreaId(areaId);
		mytimeType.setSubAreaId(subAreaId);
		if (StringUtils.isNotEmpty(timeType)) {
			if (timeType.equals("日")) {
				if (StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(upTime)) {
					mytimeType.setBeginTime(beginTime);
					mytimeType.setUpTime(upTime);
					// 1.根据时间查询出性别数据
					// 2.根据拆分方法splitUtil.splitToMap将数据拆分成Map<String,
					// Integer[]>形式（0-9，[20,30……]、10-19，[60,300……]……)
					if(ToolUtil.isNotEmpty(subAreaId)){
						List<SubAreaDmModel> subAreaAge = subAreaDmMapper.selectSubAreaAge(mytimeType);
						for (SubAreaDmModel subAreaDmModel : subAreaAge) {
							String ageUsers = subAreaDmModel.getAgeUsers();
							selectAgeList.add(ageUsers);
							String statDate = subAreaDmModel.getStatDate();
							if (map.containsKey(statDate)) {
								ArrayList<String> arrayList = map.get(statDate);
								arrayList.add(ageUsers);
							} else {
								ArrayList<String> arrayList = new ArrayList<String>();
								arrayList.add(ageUsers);
								map.put(statDate, arrayList);
							}
						}
					}else{
						List<AreaDmModel> areaAge = areaDmMapper.selectAreaAge(mytimeType);
						for (AreaDmModel areaDmModel : areaAge) {
							String ageUsers = areaDmModel.getAgeUsers();
							selectAgeList.add(ageUsers);
							String statDate = areaDmModel.getStatDate();
							if (map.containsKey(statDate)) {
								ArrayList<String> arrayList = map.get(statDate);
								arrayList.add(ageUsers);
							} else {
								ArrayList<String> arrayList = new ArrayList<String>();
								arrayList.add(ageUsers);
								map.put(statDate, arrayList);
							}
						}
					}
					// 取map中的key（时间：20171106）值，存入时间集合（timeKey）中，用工具类拆分value值
					// 放入map（convertMap）中，convertMap结构（20171106，0-9，[20,30……]、10-19，[60,300……]……，20171109，0-9，[20,30……]、10-19，[60,300……]……）
					for (Entry<String, ArrayList<String>> entry : map.entrySet()) {
						String key = entry.getKey();
						timeKey.add(key);
						ArrayList<String> arrayList = map.get(key);
						Map<String, List<Integer>> hashMap = MySplitUtilToList.splitToMap(arrayList);
						convertMap.put(key, hashMap);
					}
					Collections.sort(timeKey);
					splitToMap = MySplitUtilToList.splitToMap(selectAgeList);// 柱状图数据
				}
			}

			if (timeType.equals("月")) {
				if (StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(upTime)) {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
					DateFormat fmtDateTime = new SimpleDateFormat("yyyyMMdd");
					Date date = null;
					try {
						date = fmtDateTime.parse(beginTime);
					} catch (ParseException e) {
						e.printStackTrace();
					}
					String firstday = null;
					String lastday = null;
					Calendar cale = Calendar.getInstance();
					cale.setTime(date);
					// 获取beginTime的最后一天
					cale.add(Calendar.MONTH, 1);
					cale.set(Calendar.DAY_OF_MONTH, 0);
					lastday = sdf.format(cale.getTime());
					mytimeType.setBeginTime(beginTime);
					mytimeType.setUpTime(lastday);
					if(ToolUtil.isNotEmpty(subAreaId)){
						List<SubAreaDmModel> subAreaAge = subAreaDmMapper.selectSubAreaAge(mytimeType);
						for (SubAreaDmModel subAreaDmModel : subAreaAge) {
							String ageUsers = subAreaDmModel.getAgeUsers();
							ageListBeginMonth.add(ageUsers);
						}
					}else{
						List<AreaDmModel> areaAge = areaDmMapper.selectAreaAge(mytimeType);
						for (AreaDmModel areaDmModel : areaAge) {
							String ageUsers = areaDmModel.getAgeUsers();
							ageListBeginMonth.add(ageUsers);
						}
					}
					try {
						date = fmtDateTime.parse(upTime);
					} catch (ParseException e) {
						e.printStackTrace();
					}
					cale.setTime(date);
					cale.add(Calendar.MONTH, 0);
					cale.set(Calendar.DAY_OF_MONTH, 1);
					firstday = sdf.format(cale.getTime());
					mytimeType.setBeginTime(firstday);
					mytimeType.setUpTime(upTime);
					if(ToolUtil.isNotEmpty(subAreaId)){
						List<SubAreaDmModel> subAreaAge = subAreaDmMapper.selectSubAreaAge(mytimeType);
						for (SubAreaDmModel subAreaDmModel : subAreaAge) {
							String ageUsers = subAreaDmModel.getAgeUsers();
							ageListUpMonth.add(ageUsers);
						}
					}else{
						List<AreaDmModel> areaAge = areaDmMapper.selectAreaAge(mytimeType);
						for (AreaDmModel areaDmModel : areaAge) {
							String ageUsers = areaDmModel.getAgeUsers();
							ageListUpMonth.add(ageUsers);
						}
					}
					
					// 1.根据时间查询出性别数据
					// 2.根据拆分方法splitUtil.splitToMap将数据拆分成Map<String,
					// Integer[]>形式（0-9，[20,30……]、10-19，[60,300……]……)
					// 3.将查询时间2017-9-20,2017-10-15格式化为2017-9，2017-10形式
					// 4.查询出2017-9月份的20号至9月最后一天的数据
					// 5.查询出2017-10月份的1号到15的数据
					// 6.根据时间2017-9-20,2017-10-15查询出数据（柱状图数据）
					// 5.将结果封装到对应的集合中
					// 根据时间查询（柱状图数据）
					mytimeType.setBeginTime(beginTime);
					mytimeType.setUpTime(upTime);
					if(ToolUtil.isNotEmpty(subAreaId)){
						List<SubAreaDmModel> subAreaAge = subAreaDmMapper.selectSubAreaAge(mytimeType);
						for (SubAreaDmModel subAreaDmModel : subAreaAge) {
							String ageUsers = subAreaDmModel.getAgeUsers();
							selectAgeList.add(ageUsers);
						}
					}else{
						List<AreaDmModel> areaAge = areaDmMapper.selectAreaAge(mytimeType);
						for (AreaDmModel areaDmModel : areaAge) {
							String ageUsers = areaDmModel.getAgeUsers();
							selectAgeList.add(ageUsers);
						}
					}
					splitToMap = MySplitUtilToList.splitToMap(selectAgeList);
					String formatBeginTime = beginTime.substring(0, 6);
					String formatUpTime = upTime.substring(0, 6);
					if (ageListBeginMonth.size() > 0 || ageListUpMonth.size() > 0) {
						ageTimeList.add(formatBeginTime);
						ageTimeList.add(formatUpTime);
					}
					splitToMapMonBegin = MySplitUtilToList.splitToMap(ageListBeginMonth);
					splitToMapMonUp = MySplitUtilToList.splitToMap(ageListUpMonth);
				}
			}
		}
		// 遍历时间集合（timeKey）
		Map<String, List<Integer>> splitToMapList = new LinkedHashMap<String, List<Integer>>();
		List<String> ageSegmentationList = new ArrayList<String>();
		ageSegmentationList.add("0-9");
		ageSegmentationList.add("10-19");
		ageSegmentationList.add("20-29");
		ageSegmentationList.add("30-39");
		ageSegmentationList.add("40-49");
		ageSegmentationList.add("50-59");
		ageSegmentationList.add("60+");
		for (int i = 0; i < timeKey.size(); i++) {
			String string = timeKey.get(i);// string：时间段20171106
			ageTimeList.add(string);
			Map<String, List<Integer>> hashMap = convertMap.get(string);// 根据时间取value（0-9，[20,30……]、10-19，[60,300……]……）
			for (int j = 0; j < ageSegmentationList.size(); j++) {
				String keyStr = ageSegmentationList.get(j);
				if (hashMap.containsKey(keyStr)) {
					continue;
				} else {
					List<Integer> arrayList = new ArrayList<Integer>();
					arrayList.add(0);
					hashMap.put(keyStr, arrayList);
				}
			}
			
			for (Entry<String, List<Integer>> entry : hashMap.entrySet()) {
				String key = entry.getKey();// key：年龄分段 0-9……
				List<Integer> list = hashMap.get(key);// 根据key取value
				Integer sum = 0;
				for (Integer integer : list) {
					sum = sum + integer;
				}
				List<Integer> sumlist = new ArrayList<Integer>();
				sumlist.add(sum);
				if (splitToMapList.containsKey(key)) {
					List<Integer> splitList = splitToMapList.get(key);
					splitList.add(sum);
				} else {
					List<Integer> arrayList = new ArrayList<Integer>();
					arrayList.add(sum);
					splitToMapList.put(key, sumlist);
				}
			}
		}

		// 1.根据拆分的map取出其key值放入到集合中
		// 2.对集合进行排序Collections.sort(mapKey);
		// 3.遍历排序后的集合，根据key值取拆分的map中的value值进行求和
		List<String> mapKey = new ArrayList<>();
		for (Entry<String, List<Integer>> entry : splitToMap.entrySet()) {
			String key = entry.getKey();
			mapKey.add(key);
		}
		Collections.sort(mapKey);
		for (int j = 0; j < mapKey.size(); j++) {
			String key = mapKey.get(j);
			List<Integer> list = splitToMap.get(key);
			Integer sum = 0;
			for (Integer integer : list) {
				sum = sum + integer;
			}
			ageSegmentation.add(key);
			sumListAge.add(sum);
		}

		// 对相应的月份的值进行求和
		for (Entry<String, List<Integer>> entry : splitToMapMonUp.entrySet()) {
			String key = entry.getKey();
			List<Integer> value = entry.getValue();
			Integer sum = 0;
			for (Integer integer : value) {
				sum = sum + integer;
			}
			// ageSegmentation.add(key);
			sumListBeginTime.add(sum);
			ageUpMap.put(key, sum);// ageBeginMonthMap
		}

		// 对相应的月份的值进行求和
		for (Entry<String, List<Integer>> entry : splitToMapMonBegin.entrySet()) {
			String key = entry.getKey();
			List<Integer> value = entry.getValue();
			Integer sum = 0;
			for (Integer integer : value) {
				sum = sum + integer;
			}
			sumListUptime.add(sum);// ageBeginMonthMap
			ageBeginMonthMap.put(key, sum);// ageUpMap
		}

		// 按月查询的数据，折线图数据（2017-9,2017-10）
		for (int i = 0; i < ageSegmentation.size(); i++) {
			String key = ageSegmentation.get(i);
			Integer ageBegin = ageBeginMonthMap.get(key);// 查询开始时间的月份数据
			Integer ageUp = ageUpMap.get(key);// 查询结束时间的月份数据
			List<Integer> list = new ArrayList<>();
			if (ageBegin == null) {
				list.add(0);
			} else {
				list.add(ageBegin);
			}
			if (ageUp == null) {
				list.add(0);
			} else {
				list.add(ageUp);
			}
			allAgeMap.put(key, (ArrayList<Integer>) list);
		}
		age.setAgeSegmentationTimes(ageTimeList);
		age.setAgeSegmentation(ageSegmentation);
		age.setSplitToMapList(splitToMapList);
		age.setSumAgeList(sumListAge);
		age.setSumListBeginTime(sumListBeginTime);
		age.setSumListUptime(sumListUptime);
		age.setAllAgeMap(allAgeMap);
		return age;
	}

	/**
	 * 加载籍贯分布图表数据
	 * 
	 * @param beginTime
	 *            时间
	 * @return
	 */
	@RequestMapping(value = "/nativePoupation")
	@ResponseBody
	public Object nativePoupation(String beginTime, String upTime, String areaType, String areaId, String subAreaId) {
		if (ToolUtil.isEmpty(areaId) || ToolUtil.isEmpty(beginTime) || ToolUtil.isEmpty(upTime)) {
			return null;
		}
		MyTimeType mytimeType = new MyTimeType();
		mytimeType.setAreaId(areaId);
		mytimeType.setSubAreaId(subAreaId);
		NativePopulation nativePopulation = new NativePopulation();
		List<String> stringList = new ArrayList<String>();// 籍贯分布解析集合
		List<String> areaList = new ArrayList<String>();// 区域信息集合
		Map<String, List<Integer>> splitToMap = new HashMap<>();
		List<Integer> sumListOrigin = new ArrayList<Integer>();// 求和后的list
		List<TraceDay> traceDayList = new ArrayList<>();
		if (areaType.equals("省份")) {
			mytimeType.setBeginTime(beginTime);
			mytimeType.setUpTime(upTime);
			if (StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(upTime)) {
				traceDayList = traceDayMapper.selectHometownsProvinceBytime(mytimeType);
				for (TraceDay traceDay : traceDayList) {
					String hometownsPersonProvince = traceDay.getHometownsPersonProvince();
					stringList.add(hometownsPersonProvince);
				}
				splitToMap = MySplitUtilToList.splitToMap(stringList);
			}
		}
		if (areaType.equals("城市")) {
			mytimeType.setBeginTime(beginTime);
			mytimeType.setUpTime(upTime);
			if (StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(upTime)) {
				traceDayList = traceDayMapper.selectHometownsCityBytime(mytimeType);
				for (TraceDay traceDay : traceDayList) {
					String hometownsPersonCity = traceDay.getHometownsPersonCity();
					stringList.add(hometownsPersonCity);
				}
				splitToMap = MySplitUtilToList.splitToMap(stringList);
			}
		}
		List<String> mapKey = new LinkedList<>();
		for (Entry<String, List<Integer>> entry : splitToMap.entrySet()) {
			String key = entry.getKey();
			mapKey.add(key);
		}

		Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
		for (Entry<String, List<Integer>> entry : splitToMap.entrySet()) {
			String key = entry.getKey();
			List<Integer> value = entry.getValue();
			Integer sum = 0;
			for (Integer integer : value) {
				sum = sum + integer;
			}
			sortedMap.put(key, sum);
		}
		Comparator<Map.Entry<String, Integer>> valueComparator = new Comparator<Map.Entry<String, Integer>>() {
			@Override
			public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
				return o2.getValue() - o1.getValue();
			}
		};

		// map转换成list进行排序
		// 将排序后的map中的key值取出放入list（sortedMapKey）中
		List<Map.Entry<String, Integer>> listMap = new ArrayList<Map.Entry<String, Integer>>(sortedMap.entrySet());
		Collections.sort(listMap, valueComparator);
		int i = 0;
		Integer sumValue = 0;
		int size = listMap.size();
		for (Map.Entry<String, Integer> entry : listMap) {
			String key = entry.getKey();
			Integer value = entry.getValue();
			if (i < 10) {
				sumListOrigin.add(value);
				areaList.add(key);
			} else {
				sumValue = sumValue + value;
			}
			i++;
			if (i == size) {
				areaList.add("其它");
				sumListOrigin.add(sumValue);
			}
		}
		nativePopulation.setNativePopulationDate(sumListOrigin);
		nativePopulation.setNativePopulationName(areaList);
		return nativePopulation;
	}

	/**
	 * 加载终端分布图表数据
	 * 
	 * @param beginTime
	 *            时间
	 * @return
	 */
	@RequestMapping(value = "/terminal")
	@ResponseBody
	public Object terminalPoupation(String beginTime, String upTime, String areaId, String subAreaId) {
		if (ToolUtil.isEmpty(beginTime) || ToolUtil.isEmpty(upTime)) {
			return null;
		}
		MyTimeType mytimeType = new MyTimeType();
		mytimeType.setAreaId(areaId);
		mytimeType.setSubAreaId(subAreaId);
		TerminalPopulation terminalPopulation = new TerminalPopulation();
		List<String> stringList = new ArrayList<String>();// 终端品牌解析集合
		List<String> terminalList = new ArrayList<String>();// 终端品牌信息集合
		Map<String, List<Integer>> splitToMap = new LinkedHashMap<>();
		List<Integer> sumListTerminal = new ArrayList<Integer>();// 求和后的list
		mytimeType.setBeginTime(beginTime);
		mytimeType.setUpTime(upTime);
		if(ToolUtil.isNotEmpty(subAreaId)){
			List<SubAreaDmModel> subAreaTerminal = subAreaDmMapper.selectSubAreaTerminal(mytimeType);
			for (SubAreaDmModel subAreaDmModel : subAreaTerminal) {
				String termBrandUsers = subAreaDmModel.getTermBrandUsers();
				stringList.add(termBrandUsers);
			}
		}else{
			List<AreaDmModel> areaTerminal = areaDmMapper.selectAreaTerminal(mytimeType);
			for (AreaDmModel areaDmModel : areaTerminal) {
				String termBrandUsers = areaDmModel.getTermBrandUsers();
				stringList.add(termBrandUsers);
			}
		}
		splitToMap = MySplitUtilToList.splitToMap(stringList);
		List<String> mapKey = new LinkedList<>();
		for (Entry<String, List<Integer>> entry : splitToMap.entrySet()) {
			String key = entry.getKey();
			mapKey.add(key);
		}
		Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
		for (Entry<String, List<Integer>> entry : splitToMap.entrySet()) {
			String key = entry.getKey();
			List<Integer> value = entry.getValue();
			Integer sum = 0;
			for (Integer integer : value) {
				sum = sum + integer;
			}
			sortedMap.put(key, sum);
		}
		Comparator<Map.Entry<String, Integer>> valueComparator = new Comparator<Map.Entry<String, Integer>>() {
			@Override
			public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
				return o2.getValue() - o1.getValue();
			}
		};

		// map转换成list进行排序
		// 将排序后的map中的key值取出放入list（sortedMapKey）中
		List<Map.Entry<String, Integer>> listMap = new ArrayList<Map.Entry<String, Integer>>(sortedMap.entrySet());
		Collections.sort(listMap, valueComparator);
		int i = 0;
		for (Map.Entry<String, Integer> entry : listMap) {
			String key = entry.getKey();
			Integer value = entry.getValue();
			sumListTerminal.add(value);
			terminalList.add(key);
			i++;
			if (i == 20) {
				break;
			}
		}
		terminalPopulation.setTerminalPopulationDate(sumListTerminal);
		terminalPopulation.setTerminalPopulationName(terminalList);
		return terminalPopulation;
	}

	/**
	 * 加载客源地分布图表数据
	 * 
	 * @param beginTime
	 *            时间
	 * @return
	 */
	@RequestMapping(value = "/origin")
	@ResponseBody
	public Object originlPoupation(String beginTime, String upTime, String areaType, String areaId, String subAreaId) {
		if (ToolUtil.isEmpty(beginTime) || ToolUtil.isEmpty(upTime)) {
			return null;
		}
		MyTimeType mytimeType = new MyTimeType();
		mytimeType.setAreaId(areaId);
		mytimeType.setSubAreaId(subAreaId);
		OriginPopulation originPopulation = new OriginPopulation();
		List<String> stringList = new ArrayList<String>();// 客源地解析集合
		List<String> areaList = new ArrayList<String>();// 区域信息集合
		Map<String, List<Integer>> splitToMap = new HashMap<>();
		List<Integer> sumListOrigin = new ArrayList<Integer>();// 求和后的list
		List<SubAreaDmModel> subareaPopPropDmList = new ArrayList<SubAreaDmModel>();
		List<AreaDmModel> areaPopDmList = new ArrayList<AreaDmModel>();
		if (areaType.equals("省份")) {
			mytimeType.setBeginTime(beginTime);
			mytimeType.setUpTime(upTime);
			if (ToolUtil.isNotEmpty(subAreaId)) {
				subareaPopPropDmList = subAreaDmMapper.selectSubAreaOriginProvince(mytimeType);
				for (SubAreaDmModel subAreaDmModel : subareaPopPropDmList) {
					String provinceUsers = subAreaDmModel.getProvinceUsers();
					stringList.add(provinceUsers);
				}
			}else{
				areaPopDmList = areaDmMapper.selectAreaOriginProvince(mytimeType);
				for (AreaDmModel bdapAreaPopPropDm : areaPopDmList) {
					String provinceUsers = bdapAreaPopPropDm.getProvinceUsers();
					stringList.add(provinceUsers);
				}
			}
		}
		if (areaType.equals("城市")) {
			mytimeType.setBeginTime(beginTime);
			mytimeType.setUpTime(upTime);
			if (ToolUtil.isNotEmpty(subAreaId)) {
				List<SubAreaDmModel> subAreaDmCityList = subAreaDmMapper.selectSubAreaOriginCity(mytimeType);
				for (SubAreaDmModel subAreaDmModel : subAreaDmCityList) {
					String originPersonCity = subAreaDmModel.getOriginPersonCity();
					stringList.add(originPersonCity);
				}
			}else{
				List<AreaDmModel> selectAreaOriginCity = areaDmMapper.selectAreaOriginCity(mytimeType);
				for (AreaDmModel areaDmModel : selectAreaOriginCity) {
					String originPersonCity = areaDmModel.getOriginPersonCity();
					stringList.add(originPersonCity);
				}
			}
		}
		if (areaType.equals("区县")) {
			mytimeType.setBeginTime(beginTime);
			mytimeType.setUpTime(upTime);
			if (ToolUtil.isNotEmpty(subAreaId)) {
				List<SubAreaDmModel> subAreaDmCityList = subAreaDmMapper.selectSubAreaUserCounty(mytimeType);
				for (SubAreaDmModel subAreaDmModel : subAreaDmCityList) {
					String countyUsers = subAreaDmModel.getSnCountyUsers();
					stringList.add(countyUsers);
				}
			}else{
				List<AreaDmModel> selectAreaOriginCity = areaDmMapper.selectAreaUserCounty(mytimeType);
				for (AreaDmModel areaDmModel : selectAreaOriginCity) {
					String countyUsers = areaDmModel.getSnCountyUsers();
					stringList.add(countyUsers);
				}
			}
		}
		splitToMap = MySplitUtilToList.splitToMap(stringList);
		List<String> mapKey = new LinkedList<>();
		for (Entry<String, List<Integer>> entry : splitToMap.entrySet()) {
			String key = entry.getKey();
			mapKey.add(key);
		}

		Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
		for (Entry<String, List<Integer>> entry : splitToMap.entrySet()) {
			String key = entry.getKey();
			List<Integer> value = entry.getValue();
			Integer sum = 0;
			for (Integer integer : value) {
				sum = sum + integer;
			}
			sortedMap.put(key, sum);
		}
		Comparator<Map.Entry<String, Integer>> valueComparator = new Comparator<Map.Entry<String, Integer>>() {
			@Override
			public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
				return o2.getValue() - o1.getValue();
			}
		};

		// map转换成list进行排序
		// 将排序后的map中的key值取出放入list（sortedMapKey）中
		List<Map.Entry<String, Integer>> listMap = new ArrayList<Map.Entry<String, Integer>>(sortedMap.entrySet());
		Collections.sort(listMap, valueComparator);
		int i = 0;
		Integer sumValue = 0;
		int size = listMap.size();
		for (Map.Entry<String, Integer> entry : listMap) {
			String key = entry.getKey();
			Integer value = entry.getValue();
			if (i < 10) {
				sumListOrigin.add(value);
				areaList.add(key);
			} else {
				sumValue = sumValue + value;
			}
			i++;
			if (i == size) {
				areaList.add("其它");
				sumListOrigin.add(sumValue);
			}
		}
		originPopulation.setOriginPopulationDate(sumListOrigin);
		originPopulation.setOriginPopulationName(areaList);
		return originPopulation;
	}
	

	/**
	 * 月消费分布
	 * 
	 * @Description: TODO
	 * @param @param
	 *            beginTime
	 * @param @param
	 *            upTime
	 * @param @return
	 * @return Object
	 * @throws @author
	 *             zhangyaoyao
	 * @date 2017年11月20日
	 */
	@RequestMapping(value = "/consumption")
	@ResponseBody
	public Object consumptionPoupation(String beginTime, String upTime, String areaId, String subAreaId) {
		if (ToolUtil.isEmpty(beginTime) || ToolUtil.isEmpty(upTime)) {
			return null;
		}
		MyTimeType mytimeType = new MyTimeType();
		mytimeType.setAreaId(areaId);
		mytimeType.setSubAreaId(subAreaId);
		mytimeType.setBeginTime(beginTime);
		mytimeType.setUpTime(upTime);
		Consumption consumption = new Consumption();
		List<String> stringList = new ArrayList<String>();// 月消费额度解析集合
		List<String> consumptionList = new ArrayList<String>();// 月消费额度集合
		Map<String, List<Integer>> splitToMap = new LinkedHashMap<>();
		List<Integer> sumListConsumption = new ArrayList<Integer>();// 求和后的list
		if(ToolUtil.isNotEmpty(subAreaId)){
			List<SubAreaDmModel> subAreaConsumption = subAreaDmMapper.selectSubAreaConsumption(mytimeType);
			for (SubAreaDmModel subAreaDmModel : subAreaConsumption) {
				String arpuUsers = subAreaDmModel.getArpuUsers();
				stringList.add(arpuUsers);
			}
		}else{
			List<AreaDmModel> areaConsumption = areaDmMapper.selectAreaConsumption(mytimeType);
			for (AreaDmModel areaDmModel : areaConsumption) {
				String arpuUsers = areaDmModel.getArpuUsers();
				stringList.add(arpuUsers);
			}
		}
		splitToMap = MySplitUtilToList.splitToMap(stringList);
		// 对拆分后的map集合进行排序以及求和
		// 取出splitToMap中的key值（消费分布：0-10；10-20……），放入mapkey中
		List<String> mapKey = new LinkedList<>();
		for (Entry<String, List<Integer>> entry : splitToMap.entrySet()) {
			String key = entry.getKey();
			mapKey.add(key);
		}
		// 构造消费水平分布map，根据value进行排序
		Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
		for (int i = 0; i < mapKey.size(); i++) {
			String string = mapKey.get(i);
			int indexOf = string.indexOf("-");
			String substringStr = string.substring(indexOf + 1, string.length());
			if ("300+".equals(string)) {
				substringStr = "300";
			}
			sortedMap.put(string, Integer.parseInt(substringStr));
		}
		Comparator<Map.Entry<String, Integer>> valueComparator = new Comparator<Map.Entry<String, Integer>>() {
			@Override
			public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
				return o1.getValue() - o2.getValue();
			}
		};

		// map转换成list进行排序
		// 将排序后的map中的key值取出放入list（sortedMapKey）中
		List<Map.Entry<String, Integer>> listMap = new ArrayList<Map.Entry<String, Integer>>(sortedMap.entrySet());
		Collections.sort(listMap, valueComparator);
		List<String> sortedMapKey = new LinkedList<>();
		for (Map.Entry<String, Integer> entry : listMap) {
			String key = entry.getKey();
			sortedMapKey.add(key);
		}
		// 根据list（sortedMapKey）中的值，去splitToMap中取对应key值的value进行求和计算
		for (int j = 0; j < sortedMapKey.size(); j++) {
			String key = sortedMapKey.get(j);
			List<Integer> value = splitToMap.get(key);
			Integer sum = 0;
			for (Integer integer : value) {
				sum = sum + integer;
			}
			sumListConsumption.add(sum);
			consumptionList.add(key);
		}

		consumption.setConsumptionDate(sumListConsumption);
		consumption.setConsumptionName(consumptionList);
		return consumption;

	}

	/**
	 * 逗留情况图表
	 * 
	 * @Description: TODO
	 * @param @param
	 *            beginTime
	 * @param @param
	 *            upTime
	 * @param @param
	 *            areaId
	 * @param @param
	 *            subAreaId
	 * @param @return
	 * @return Object
	 * @throws @author
	 *             zhangyaoyao
	 * @date 2018年1月8日
	 */
	@RequestMapping(value = "/staySituation")
	@ResponseBody
	public Object stayPoupation(String beginTime, String areaId, String subAreaId, String timeType) {
		if (ToolUtil.isEmpty(beginTime) || ToolUtil.isEmpty(timeType)) {
			return null;
		}
		MyTimeType mytimeType = new MyTimeType();
		mytimeType.setAreaId(areaId);
		mytimeType.setSubAreaId(subAreaId);
		StaySituation staySituation = new StaySituation();
		List<String> stringList = new ArrayList<String>();// 逗留情况解析集合
		List<String> staySituationList = new ArrayList<String>();// 逗留情况分布用户数集合
		Map<String, List<Integer>> splitToMap = new LinkedHashMap<>();
		List<Integer> sumListStaySituation = new ArrayList<Integer>();// 求和后的list
		if (timeType.equals("天")) {
			mytimeType.setBeginTime(beginTime);
			if (ToolUtil.isNotEmpty(subAreaId)) {
				List<BdapSubareaStayMm> subAreaStayMM = subAreaStayMmMapper.selectSubAreaStayMM(mytimeType);
				for (BdapSubareaStayMm bdapSubareaStayMm : subAreaStayMM) {
					String stayTimeUsers = bdapSubareaStayMm.getStayTimeUsers();
					stringList.add(stayTimeUsers);
				}
			}else{
				List<BdapAreaStayMm> areaStayMM = areaStayMmMapper.selectAreaStayMM(mytimeType);
				for (BdapAreaStayMm bdapAreaStayMm : areaStayMM) {
					String stayTimeUsers = bdapAreaStayMm.getStayTimeUsers();
					stringList.add(stayTimeUsers);
				}
			}
			splitToMap = MySplitUtilToList.splitToMap(stringList);
		}
		if (timeType.equals("小时")) {
			mytimeType.setBeginTime(beginTime);
			if (ToolUtil.isNotEmpty(subAreaId)) {
				List<BdapSubareaPopPropDm> selectStaySituationDm = subAreaDmMapper.selectStaySituationDm(mytimeType);
				for (BdapSubareaPopPropDm bdapSubareaPopPropDm : selectStaySituationDm) {
					String stayTimeUsers = bdapSubareaPopPropDm.getStayTimeUsers();
					stringList.add(stayTimeUsers);
				}
			}else{
				List<BdapAreaPopPropDm> selectAreaStaySituationDm = areaDmMapper.selectAreaStaySituationDm(mytimeType);
				for (BdapAreaPopPropDm bdapAreaPopPropDm : selectAreaStaySituationDm) {
					String stayTimeUsers = bdapAreaPopPropDm.getStayTimeUsers();
					stringList.add(stayTimeUsers);
				}
			}
			splitToMap = MySplitUtilToList.splitToMap(stringList);
		}
		List<String> mapKey = new LinkedList<>();
		for (Entry<String, List<Integer>> entry : splitToMap.entrySet()) {
			String key = entry.getKey();
			mapKey.add(key);
		}
		// 构造消费水平分布map，根据value进行排序
		Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
		for (int i = 0; i < mapKey.size(); i++) {
			String string = mapKey.get(i);
			int indexOf = string.indexOf("-");
			String substringStr = string.substring(indexOf + 1, string.length());
			if (string.contains("+")) {
				substringStr = "300";
			}
			sortedMap.put(string, Integer.parseInt(substringStr));
		}
		Comparator<Map.Entry<String, Integer>> valueComparator = new Comparator<Map.Entry<String, Integer>>() {
			@Override
			public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
				return o1.getValue() - o2.getValue();
			}
		};

		// map转换成list进行排序
		// 将排序后的map中的key值取出放入list（sortedMapKey）中
		List<Map.Entry<String, Integer>> listMap = new ArrayList<Map.Entry<String, Integer>>(sortedMap.entrySet());
		Collections.sort(listMap, valueComparator);
		List<String> sortedMapKey = new LinkedList<>();
		for (Map.Entry<String, Integer> entry : listMap) {
			String key = entry.getKey();
			sortedMapKey.add(key);
		}
		// 根据list（sortedMapKey）中的值，去splitToMap中取对应key值的value进行求和计算
		for (int j = 0; j < sortedMapKey.size(); j++) {
			String key = sortedMapKey.get(j);
			List<Integer> value = splitToMap.get(key);
			Integer sum = 0;
			for (Integer integer : value) {
				sum = sum + integer;
			}
			sumListStaySituation.add(sum);
			staySituationList.add(key);
		}
		staySituation.setStaySituationDate(sumListStaySituation);
		staySituation.setStaySituationName(staySituationList);
		return staySituation;

	}

	/**
	 * 忠诚度图表
	 * 
	 * @Description: TODO
	 * @param @param
	 *            beginTime
	 * @param @param
	 *            areaId
	 * @param @param
	 *            subAreaId
	 * @param @return
	 * @return Object
	 * @throws @author
	 *             zhangyaoyao
	 * @date 2018年1月9日
	 */
	@RequestMapping(value = "/loyaltySituation")
	@ResponseBody
	public Object loyaltyPoupation(String beginTime, String areaId, String subAreaId) {
		if (ToolUtil.isEmpty(beginTime)) {
			return null;
		}
		MyTimeType mytimeType = new MyTimeType();
		mytimeType.setAreaId(areaId);
		mytimeType.setSubAreaId(subAreaId);
		LoyaltySituation loyaltySituation = new LoyaltySituation();
		List<String> stringList = new ArrayList<String>();// 忠诚度解析集合
		List<String> staySituationList = new ArrayList<String>();// 忠诚度分布用户数集合
		Map<String, List<Integer>> splitToMap = new LinkedHashMap<>();
		List<Integer> sumListStaySituation = new ArrayList<Integer>();// 求和后的list
		mytimeType.setBeginTime(beginTime);
		if (StringUtils.isNotEmpty(subAreaId)) {
			List<BdapSubareaLoyaltyMm> subArealoyalty = subAreaLoyaltyMapper.selectSubArealoyaltyPoupation(mytimeType);
			for (BdapSubareaLoyaltyMm bdapSubareaLoyaltyMm : subArealoyalty) {
				String visitUsers = bdapSubareaLoyaltyMm.getVisitUsers();
				stringList.add(visitUsers);
			}
		}else{
			List<BdapAreaLoyaltyMm> arealoyalty = areaLoyaltyMapper.selectArealoyaltyPoupation(mytimeType);
			for (BdapAreaLoyaltyMm bdapAreaLoyaltyMm : arealoyalty) {
				String visitUsers = bdapAreaLoyaltyMm.getVisitUsers();
				stringList.add(visitUsers);
			}
		}
		splitToMap = MySplitUtilToList.splitToMap(stringList);
		List<String> mapKey = new LinkedList<>();
		for (Entry<String, List<Integer>> entry : splitToMap.entrySet()) {
			String key = entry.getKey();
			mapKey.add(key);
		}
		Collections.sort(mapKey);
		// 根据list（sortedMapKey）中的值，去splitToMap中取对应key值的value进行求和计算
		for (int j = 0; j < mapKey.size(); j++) {
			String key = mapKey.get(j);
			List<Integer> value = splitToMap.get(key);
			Integer sum = 0;
			for (Integer integer : value) {
				sum = sum + integer;
			}
			sumListStaySituation.add(sum);
			staySituationList.add(key);
		}
		loyaltySituation.setLoyaltySituationDate(sumListStaySituation);
		loyaltySituation.setLoyaltySituationName(staySituationList);
		return loyaltySituation;

	}

	/**
	 * 子区域平均逗留时长
	 * 
	 * @Description: TODO
	 * @param @param
	 *            beginTime
	 * @param @param
	 *            areaId
	 * @param @param
	 *            subAreaId
	 * @param @param
	 *            timeType
	 * @param @return
	 * @return Object
	 * @throws @author
	 *             zhangyaoyao
	 * @date 2018年3月12日
	 */
	@RequestMapping(value = "/staySituationRanking")
	@ResponseBody
	public Object staySituationRanking(String beginTime, String areaId, String subAreaId, String timeType) {
		if (ToolUtil.isEmpty(areaId) || ToolUtil.isEmpty(beginTime) || ToolUtil.isEmpty(timeType)) {
			return null;
		}
		StayTimeRanking stayTimeRanking = new StayTimeRanking();
		List<String> subAreaName = new ArrayList<String>();// 子区域
		List<String> stayAvgTime = new ArrayList<String>();// 子区域平均逗留时间
		MyTimeType mytimeType = new MyTimeType();
		Map<String, String> stayAvgMap = new HashMap<String, String>();// 子区域平均逗留时间
		Map<String, Double> stayHourMap = new HashMap<String, Double>();// 逗留情况时间粒度
		if (timeType.equals("小时")) {
			stayHourMap.put("0-2", 1.0);
			stayHourMap.put("2-4", 3.0);
			stayHourMap.put("4-6", 5.0);
			stayHourMap.put("6-8", 7.0);
			stayHourMap.put("8-10", 9.0);
			stayHourMap.put("10-12", 11.0);
			stayHourMap.put("12+", 12.0);
		} else {
			stayHourMap.put("0-5", 2.5);
			stayHourMap.put("5-10", 7.5);
			stayHourMap.put("10-15", 12.5);
			stayHourMap.put("15-20", 17.5);
			stayHourMap.put("20-25", 22.5);
			stayHourMap.put("25+", 25.0);
		}
		List<Map.Entry<String, Double>> compareStayHourMap = (List<Entry<String, Double>>) stayHourMap.entrySet()
				.stream().sorted(Map.Entry.comparingByValue()).collect(Collectors.toList());
		List<BdapSubArea> subAreaList = new ArrayList<BdapSubArea>();
		Wrapper<BdapSubArea> subAreaWrapper = new EntityWrapper<BdapSubArea>();
		subAreaWrapper.eq("pid", areaId);
		subAreaList = subareaMapper.selectList(subAreaWrapper);
		if (StringUtils.isNotEmpty(beginTime)) {
			mytimeType.setBeginTime(beginTime);
			for (int i = 0; i < subAreaList.size(); i++) {
				List<String> stringList = new ArrayList<String>();// 逗留情况解析集合
				BdapSubArea bdapSubArea = subAreaList.get(i);
				String name = bdapSubArea.getName();
				Long rankingSubAreaId = bdapSubArea.getId();
				Long rankingId = bdapSubArea.getPid();
				mytimeType.setAreaId(String.valueOf(rankingId));
				mytimeType.setSubAreaId(String.valueOf(rankingSubAreaId));
				if (timeType.equals("天")) {
					mytimeType.setBeginTime(beginTime);
					if (ToolUtil.isNotEmpty(subAreaId)) {
						List<BdapSubareaStayMm> subAreaStayMM = subAreaStayMmMapper.selectSubAreaStayMM(mytimeType);
						for (BdapSubareaStayMm bdapSubareaStayMm : subAreaStayMM) {
							String stayTimeUsers = bdapSubareaStayMm.getStayTimeUsers();
							stringList.add(stayTimeUsers);
						}
					}else{
						List<BdapAreaStayMm> areaStayMM = areaStayMmMapper.selectAreaStayMM(mytimeType);
						for (BdapAreaStayMm bdapAreaStayMm : areaStayMM) {
							String stayTimeUsers = bdapAreaStayMm.getStayTimeUsers();
							stringList.add(stayTimeUsers);
						}
					}
				}
				if (timeType.equals("小时")) {
					mytimeType.setBeginTime(beginTime);
					if (ToolUtil.isNotEmpty(subAreaId)) {
						List<BdapSubareaPopPropDm> selectStaySituationDm = subAreaDmMapper.selectStaySituationDm(mytimeType);
						for (BdapSubareaPopPropDm bdapSubareaPopPropDm : selectStaySituationDm) {
							String stayTimeUsers = bdapSubareaPopPropDm.getStayTimeUsers();
							stringList.add(stayTimeUsers);
						}
					}else{
						List<BdapAreaPopPropDm> selectAreaStaySituationDm = areaDmMapper.selectAreaStaySituationDm(mytimeType);
						for (BdapAreaPopPropDm bdapAreaPopPropDm : selectAreaStaySituationDm) {
							String stayTimeUsers = bdapAreaPopPropDm.getStayTimeUsers();
							stringList.add(stayTimeUsers);
						}
					}
				}
				Map<String, List<Integer>> splitToMap = MySplitUtilToList.splitToMap(stringList);
				for (String key : stayHourMap.keySet()) {
					if (splitToMap.containsKey(key)) {
						continue;
					} else {
						List<Integer> list = new ArrayList<Integer>();
						list.add(0);
						splitToMap.put(key, list);
					}
				}
				Double sumTimeValue = 0d;
				Double sumValue = 0d;
				for (Entry<String, Double> entry : compareStayHourMap) {
					String key = entry.getKey();
					Double stayHourValue = entry.getValue();
					List<Integer> value = splitToMap.get(key);
					Integer sum = 0;
					for (Integer integer : value) {
						sum = sum + integer;
					}
					sumTimeValue = stayHourValue * sum + sumTimeValue;
					sumValue = sumValue + sum;
				}
				String avgStayTime = null;
				DecimalFormat df = new DecimalFormat("0.0");
				if (sumValue != 0) {
					avgStayTime = df.format((double) sumTimeValue / sumValue);
					stayAvgMap.put(name, avgStayTime);
				}
				if (stayAvgMap.size() == 0) {
					return null;
				}
			}
			List<Map.Entry<String, String>> compareByValue = (List<Entry<String, String>>) stayAvgMap.entrySet()
					.stream().sorted(Map.Entry.comparingByValue()).collect(Collectors.toList());
			for (Entry<String, String> entry : compareByValue) {
				subAreaName.add(entry.getKey());
				stayAvgTime.add(entry.getValue());
			}
		}
		stayTimeRanking.setStayRankingName(subAreaName);
		stayTimeRanking.setStayRankingTime(stayAvgTime);
		return stayTimeRanking;
	}

	/**
	 * 查找区域id
	 * 
	 * @Description: TODO
	 * @param @param
	 *            beginTime
	 * @param @param
	 *            upTime
	 * @param @return
	 * @return Object
	 * @throws @author
	 *             zhangyaoyao
	 * @date 2017年11月28日
	 */
	@RequestMapping(value = "/findName")
	@ResponseBody
	public Object findAreaID(String areaId, String subAreaId) {
		JSONArray jsonArray = new JSONArray();
		try {
			if (ToolUtil.isEmpty(areaId)) {
				return null;
			}
			LonLat lonLatModel = new LonLat();
			BdapArea area = new BdapArea();
			List<BdapSubArea> subAreaList = new ArrayList<BdapSubArea>();
			Wrapper<BdapSubArea> subAreaWrapper = new EntityWrapper<BdapSubArea>();
			area = areaMapper.selectById(Long.parseLong(areaId));
			if (ToolUtil.isEmpty(subAreaId)) {
				subAreaWrapper.eq("pid", areaId);
				subAreaList = subareaMapper.selectList(subAreaWrapper);
			} else {
				subAreaWrapper.eq("id", subAreaId);
				subAreaList = subareaMapper.selectList(subAreaWrapper);
			}
			List<Object> times = cellpopHmMapper
					.selectObjs(new EntityWrapper<BdapCellPopPropHm>().setSqlSelect("max(STAT_HOUR)"));
			String maxTime = times.get(0).toString();
			if (ToolUtil.isNotEmpty(subAreaList)) {
				// 新增：这里进行lac,ci的拆分循环查询每个基站下面的人口数，进行统计汇总 @polly
				for (int i = 0; i < subAreaList.size(); i++) {
					String lonLat = "";
					JSONObject jbObject = new JSONObject();
					BdapSubArea bsa = subAreaList.get(i);
					String strs = bsa.getLacCi();
					int warningValue = bsa.getWarningValue();
					int saturationValue = bsa.getSaturationValue();
					int severeValue = bsa.getSevereValue();
					int totalUsers = screenMapper.selectTotalUser(areaId, bsa.getId().toString(), maxTime);
					if (ToolUtil.isNotEmpty(area)) {
						jbObject.put("cityName", area.getCity());
						jbObject.put("lon", area.getLon());
						jbObject.put("lat", area.getLat());
					} else {
						return null;
					}
					lonLat += bsa.getLonLat();
					lonLat += "&";
					lonLat = lonLat.substring(0, lonLat.length() - 1);
					jbObject.put("warningValue", warningValue);
					jbObject.put("saturationValue", saturationValue);
					jbObject.put("severeValue", severeValue);
					jbObject.put("popCount", totalUsers);
					jbObject.put("lonLat", lonLat);
					jsonArray.put(jbObject);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonArray.toString();
	}
}
